I do not understand the logic of the implicit casting of the int to Enum.
class Program
{
static void Main(string[] args)
{
TestMethod((int)0); //ok
TestMethod((int)1); //compile error it is not assignable
}
private static void TestMethod(SuperEnum e)
{
//...
}
}
public enum SuperEnum : int
{
None = 0,
One,
Two
}
Why zero is acceptable but any integers are not?
The MSIL listing of the first line looks the same for
TestMethod(0) and TestMethod((SuperEnum)1)
// [12 9 - 12 10]
IL_0000: nop
// [13 12 - 13 31]
IL_0001: ldc.i4.0 //load 0 or 1
IL_0002: call void ConsoleApp2.Program::TestMethod(valuetype ConsoleApp2.SuperEnum)
IL_0007: nop
// [15 9 - 15 10]
IL_0008: ret
Thanks!