31

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

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
franko_camron
  • 1,288
  • 4
  • 14
  • 30

6 Answers6

51

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();
}
AaronLS
  • 37,329
  • 20
  • 143
  • 202
ken2k
  • 48,145
  • 10
  • 116
  • 176
  • Assuming you haven't defined your enum as `public enum MyEnum: long`, in which case casting to int might result in truncation. – Chris Shain Jan 17 '12 at 18:26
  • Since title of the question is about casting enum to int, and the marked answer didn't actually include that as part of the code example, I added that. – AaronLS Apr 16 '13 at 23:30
  • 4
    Although not required, I STRONGLY recommend assigning an int value to each member in the enum. If you don't then the first member is 0, the second is 1, up to N. The problem is that some environments sort your enumeration while others don't. This will result in a different int values on different environments. For me, it was my production environment sorting the enum while my localhost didn't. This caused a bug in production that I couldn't reproduce in my dev environment. – Bob Jul 02 '13 at 21:12
14

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.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
13

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.

Taryn
  • 242,637
  • 56
  • 362
  • 405
  • 4
    OP is trying to go the other way. He has an integer value and needs to find the corresponding enum value. – Yuck Jan 17 '12 at 18:14
  • ah, i see that now, thanks for pointing it out. This could still be used to see if the value submitted equals the value of the enum – Taryn Jan 17 '12 at 18:18
1
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

IUnknown
  • 888
  • 7
  • 18
1

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.

3Dave
  • 28,657
  • 18
  • 88
  • 151
-1

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.

Leon
  • 3,311
  • 23
  • 20