Is it possible to cast integer to enum? [duplicate]

泄露秘密 提交于 2019-12-28 13:41:02

问题


I got the following enum:

 public enum detallistaDocumentStatus {

    /// <remarks/>
    ORIGINAL,

    /// <remarks/>
    COPY,

    /// <remarks/>
    REEMPLAZA,

    /// <remarks/>
    DELETE,
}

then I got a class property of type detallistaDocumentStatus:

 public detallistaDocumentStatus documentStatus {
        get {
            return this.documentStatusField;
        }
        set {
            this.documentStatusField = value;
        }
    }

In the real life the user will send us a number (1, 2, 3 or 4) representing each enum value in the order they are declared.

so, is it possible to cast like this?

det.documentStatus = (detallistaDocumentStatus)3;

if not, how could I get the enum value using an integer as an index, we are using a lot of enums, so we want to do something generic and reusable


回答1:


Yes, it's possible to cast Enum to int and vice versa, because every Enum is actually represented by an int per default. You should manually specify member values. By default it starts from 0 to N.

It's also possible to cast Enum to string and vice versa.

public enum MyEnum
{
    Value1 = 1,
    Value2 = 2,
    Value3 = 3
}

private static void Main(string[] args)
{
    int enumAsInt = (int)MyEnum.Value2; //enumAsInt == 2

    int myValueToCast = 3;
    string myValueAsString = "Value1";
    MyEnum myValueAsEnum = (MyEnum)myValueToCast;   // Will be Value3

    MyEnum myValueAsEnumFromString;
    if (Enum.TryParse<MyEnum>(myValueAsString, out myValueAsEnumFromString))
    {
        // Put logic here
        // myValueAsEnumFromString will be Value1
    }

    Console.ReadLine();
}



回答2:


Yes it is possible. I use the following ENUM

public enum AccountTypes
{
  Proposed = 1,
  Open = 2
}

then when I call it I use this to get the value:

(int)AccountTypes.Open

And it will return the int value that I need which for the above will be 2.




回答3:


From the C# 4.0 Specification:

1.10 Enums

Enum values can be converted to integral values and vice versa using type casts. For example

int i = (int)Color.Blue;      // int i = 2;
Color c = (Color)2;               // Color c = Color.Blue;

One additional thing to be aware of is that you are permitted to cast any integral value in the range of the enum's underlying type (by default that's int), even if that value doesn't map to one of the names in the enum declaration. From 1.10 Enums:

The set of values that an enum type can take on is not limited by its enum members. In particular, any value of the underlying type of an enum can be cast to the enum type and is a distinct valid value of that enum type.

So, the following is also permitted with the enum in your example:

det.documentStatus = (detallistaDocumentStatus) 42;

even though there's no enum name that has the value 42.




回答4:


det.documentStatus = (detallistaDocumentStatus)3;

The above should work fine. Only thing to remember here is if you are assigning specific numbers to enum members then something like this may not work as expected. Also if you are not explicitly numbering your enum values then the enum members start at zero based index.

So the above code would assign 4th member of the enum in the order of declaration.

To check whether a given integer is a valid enum value for that enum use Enum.IsDefined




回答5:


If you give an explicit integer value for your enum members, like so:

public enum Foo
{
    Five=5
    , Six
    , Seven
    , Eight
}

You can just cast them to an int to get the implicit value.

var bar = (int)Foo.Six: /// bar == 6

If you don't explicitly define a value for at least the first, casting it to an int will give you the order in which it appears in the declaration.




回答6:


ENUMs are just a convenient way to name integers. Values of an ENUM can be added, subtracted and compared the same way an int can be.



来源:https://stackoverflow.com/questions/8899498/is-it-possible-to-cast-integer-to-enum

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!