Reputation:
Is there a way i can make my enum defaulted as ints? so i dont need to typecast it everywhere? Basically i am using it as a list of const values.
Upvotes: 3
Views: 524
Reputation: 11654
If you really want to do that, you could create an extension method like the following for enums
public static int ToInt(this Enum obj)
{
return Convert.ToInt32(obj);
}
Then you could use it like the following
var t = Gender.male;
var r = t.ToInt();
Upvotes: 1
Reputation: 109210
No.
If you really need a group of int constants, then use a static class:
public static class Constants {
public const int ConstantOne = 42;
public const int ConstantTwo = 42;
...
}
Upvotes: 8
Reputation: 15795
No, if you've declared an enum, their default value is as an enum, and an explicit cast is required to get to the int value. If you're using the enum as const values, why not use const values? If you want some separation you could create a struct that contains nothing but these const values.
Upvotes: 7