System.Enum, everyday is a party with you


Enumerations should be banned for a number of reasons, or at least should be enhanced or sanitized (</flame>). I have already had my share of surprises with them, but it seems surprises are never-ending with this type. And I feel that is not right and that’s why I think enumerations should get a big re-thinking.

The last surprise came while I was testing some extensions methods to check if a boxed object has a default (as per the default keyword) value. Everything was according to plan but then I thought “oh, and why not test the code with enumerations? and what would happen with a non-zero based enumerations?”.

I know, I know, those are strange thoughts to have and maybe I deserved what I found out: default values for enumerations that do not define a zero value is zero, even if zero is not a valid value for the enumeration. Yes, you heard it. and you have probably seen it before.

Look at this code:

And now tell me I got what I deserved.

On the other hand, being such a crippled creature it offers a lot of margin for beautiful workarounds, like this one (and the linked posts). Really smart stuff for a really not-so-smart type.

That’s enough for today although I firmly believe it will not be the last time I deal with enumerations.

share class