10

Take this bit of code:

enum En {
    val1,
    val2,
}

void Main()
{
    En plop = 1;  //error: Cannot implicitly convert type 'int' to 'En'
    En woop = 0;  //no error
}

Of course it fails when assigning 1 to an enum-type variable. (Slap an explicit cast and it'll work.)

My question is: why does it NOT fail when assigning 0 ?

Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
  • 1
    **Duplicate** of [Why switch for enum accepts implicit conversion to 0 but no for any other integer?](http://stackoverflow.com/questions/14950750/why-switch-for-enum-accepts-implicit-conversion-to-0-but-no-for-any-other-intege) **and** [Compiler Value Type Resolution and Hardcoded “0” Integer Values](http://stackoverflow.com/questions/14224465/compiler-value-type-resolution-and-hardcoded-0-integer-values). – Tim Schmelter Jun 06 '13 at 11:03
  • definite dupe, short answer, because it does. – Jodrell Jun 06 '13 at 11:05
  • by default, if the value of the first enumeration member is not set in the declaration, its value is zero. http://msdn.microsoft.com/en-us/library/ms182149(v=vs.100).aspx – Guido Preite Jun 06 '13 at 11:07
  • 1
    Two really good answers here: http://stackoverflow.com/questions/14224465/compiler-value-type-resolution-and-hardcoded-0-integer-values/14238286#14238286 – DonBoitnott Jun 06 '13 at 11:08
  • @DonBoitnott and Tim Schmelter - thanks for the links. Interesting reads. – Cristian Diaconescu Jun 06 '13 at 11:15
  • 2
    I like the part of [Eric's answer](http://stackoverflow.com/questions/14224465/compiler-value-type-resolution-and-hardcoded-0-integer-values/14238286#14238286) that says *"Second, the reason for allowing zeros to convert to any enum is to ensure that it is always possible to zero out a "flags" enum... The designers of C# 1.0 thought that it looked strange that you might have to say `for (MyFlags f = (MyFlags)0; ...`"* That's the closest to a "why is the SPEC like this" answer I've seen yet. – Rawling Jun 06 '13 at 11:15
  • Also related, and by Eric Lippert: http://blogs.msdn.com/b/ericlippert/archive/2006/03/28/the-root-of-all-evil-part-one.aspx?PageIndex=1 (read the comments too!) – Cristian Diaconescu Jun 06 '13 at 11:31

2 Answers2

11

It's this way because that's what the spec says...

This is another reason why it's always a good idea to give all your enums an item with value 0, 'cos you're likely to get them with that value sometimes.


The appropriate section in the C# language spec 6.1.3:

6.1.3 Implicit enumeration conversions

An implicit enumeration conversion permits the decimal-integer-literal 0 to be converted to any enum-type and to any nullable-type whose underlying type is an enum-type. In the latter case the conversion is evaluated by converting to the underlying enum-type and wrapping the result (§4.1.10).

As to why it's that way - well, I guess only someone on the language committee that decides these things would know.

In fact, we do have something like that if you look at rawling's comment to the original question.

Community
  • 1
  • 1
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
1

The reason you can only implicitly use 0 is because 0 will always be a valid enum type where 1,2,3 or any other number might not necessarily be a valid type. For example try this

enum En {
val1=1,
val2=2,
}

0 is still a valid enum type because 0 is the default no matter what you do. Which means if you do not let one of your values be equal to 0 it will generate a 0 enum type for you.

CodeCamper
  • 6,609
  • 6
  • 44
  • 94