Please check following code tell me what the difference between Convert.ToInt32() and int(), Why Convert.ToInt32 is showing error?
Here UserType is a enum
// Showing error constant initializer must be compile time constant
const int case1 = Convert.ToInt32(UserType.Admin);
const int case2 = int(UserType.Admin);
You’re assigning a return value from a method to a constant, which is not allowed. In .Net, the value of a constant is required to be known at compile time. This is not possible if it is being assigned a value from a method call at runtime.
For the general case, you could alter this slightly and have code which is logically equivalent:
However, a simple cast to an
intis allowable with enumerations, so your second example would probably be Ok (albeit not idiomatic).