Why can I assign an int to enumerate only when it is zero?

Try this minimal code:

void Main() { Foo test; test = 0; test = 1; Console.WriteLine(test); } enum Foo { Bar, Baz } 

The string test = 1 will cause a compilation error, however the string test = 0 will be compiled with pleasure!

CS0266 Cannot implicitly convert type 'int' to 'UserQuery.Foo'. Explicit conversion exists (are you skipping listing?)

Compiler Version: Microsoft (R) Visual C # Compiler version 1.0.0.50618

Why can I assign only zero? Should this not be the missing error in both cases?

+6
source share

All Articles