Let's use the following enum as an example:
public enum AreaType
WOODLAND
SNOWY
The compiler will associate 0 with WOODLAND and 1 with SNOWY.
The compiler complains if we try to compare an enum with an integer. This is a good thing. Take for instance:
let isWoodland = AreaType.WOODLAND == 0
The above gives the error Cannot compare types AreaType with integer-literal. Exactly what we want.
Unfortunately, comparison between enum and null works. This produces unexpected results:
let isWoodland = AreaType.WOODLAND == null
isWoodland will be assigned true since null is coalesced into 0 (which coincides with the integer representation of WOODLAND). This leads to subtle bugs. Note that you can also do meaningless stuff like AreaType areaType = null.
I think comparison between enum and null should give a compiler error instead.
Let's use the following
enumas an example:The compiler will associate
0withWOODLANDand1withSNOWY.The compiler complains if we try to compare an
enumwith aninteger. This is a good thing. Take for instance:The above gives the error
Cannot compare types AreaType with integer-literal. Exactly what we want.Unfortunately, comparison between
enumandnullworks. This produces unexpected results:isWoodlandwill be assignedtruesincenullis coalesced into0(which coincides with the integer representation ofWOODLAND). This leads to subtle bugs. Note that you can also do meaningless stuff likeAreaType areaType = null.I think comparison between
enumandnullshould give a compiler error instead.