This code does not compile with the latest C # compiler :
public class Program
{
public static void Main()
{
IntEnum a = (IntEnum)-1;
}
}
public enum IntEnum : int { }
When you try to compile it, it raises
(3,22,3,29): Error CS0119: "IntEnum" is a type that is not valid in this context
Oddly enough, changing the cast value to a positive number (for example 4) or using the const value (for example int.MinValue) or even the surrounding value using parentheses such as (IntEnum)(-1)will compile and work just fine. However, the above sample does not work.
Is there a reason for this? Is it possible that Roslyn may be parsing the code incorrectly, and why does the error occur?