If I have the following enum:
public enum ReturnValue{ Success = 0, FailReason1 = 1, FailReason2 = 2 //Etc... }
Can I avoid casting when I return, like this:
public static int main(string[] args){ return (int)ReturnValue.Success; }
If not, why isn’t an enum value treated as an int by default?
enums are supposed to be type safe. I think they didn’t make them implicitly castable to discourage other uses. Although the framework allows you to assign a constant value to them, you should reconsider your intent. If you primarily use the enum for storing constant values, consider using a static class:
That lets you do this.
EDIT
When you do want to provide values to an enum is when you want to combine them. See the below example:
This enum can then be consumed by using bitwise math. See the below example for some applications.