-3

I have a problem with enum in c#

enum myenum {one,  two,  three} ; 
Public myenum type; 
type=2;

Why it doesn't work? How to cast enum to integer in such a way?

Zong
  • 6,160
  • 5
  • 32
  • 46
fragon
  • 3,391
  • 10
  • 39
  • 76

5 Answers5

2

You have to explicitly cast integer to myenum:

type = (myenum) 2;

See this thread for more explanation: Cast int to enum in C#

Community
  • 1
  • 1
King King
  • 61,710
  • 16
  • 105
  • 130
0

There is no implicit conversion between an int and enum. There is however an explicit conversion

type = 2;         // Error
type = (myenum)2; // Ok

The one exception to this rule is the literal 0. The literal 0 implicitly converts to any enum type

type = 0; // Ok 
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
0

You can explicitly convert between the two, also ensure that you declare the enums with a specific value as the automatic index starts from zero.

    enum myenum {one = 1,  two = 2,  three = 3} ; 


    private void GoEnum()
    {
        myenum x = (myenum)1;
        Console.WriteLine(x);
        Console.WriteLine((int)x);

    }
Kami
  • 19,134
  • 4
  • 51
  • 63
0

The entire purpose of enums is to avoid "magic numbers". Their purpose is to avoid code just like that in which the reader needs to just "know" that 2 represents myenum.two. Behind the scenes it is just an integer (or some other integral type) but the language works to hide that fact from you.

It does allow you to convert an integer into it's enumeration representation, and vice versa, because there are times where this is simply necessary, but because it should be avoided wherever possible the language forces you to explicitly cast the integer (type = (myenum) 2;), rather than implicitly converting it for you, so that your intentions are made very clear in code.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • but... this explicit rule doesn't applies for `0`(Zero), Zero is implicitly convertible to any enum type. – Sriram Sakthivel Dec 06 '13 at 18:20
  • @SriramSakthivel Yes, the exception needed to be made simply because there is a requirement that all value types need to be able to be initialized to a bitwise zero, so zero needed to be a valid value for any enum. If they could have avoided it, they probably would have. In general it's best to avoid relying on that exception wherever you can. – Servy Dec 06 '13 at 18:26
0

Unless specified, an enum cannot be converted to an integer without a cast (which is unsafe). You have to assign integer values to each option in the enum.

Example:

enum MyEnum {
    One,
    Two,
    Three
};

Would become:

enum MyEnum {
    One = 1,
    Two = 2,
    Three = 3
};

An enum takes up the size of an integer (4 bytes). This allows a cast to int, but like I have said it is better practise to assign values to enum options. If you wanted to change the value that MyEnum extends, do this:

enum MyEnum : long /* or short, ushort, ulong, uint, sbyte, byte etc */ {
    One = 1L,
    Two = 2L,
    Three = 3L
};

Note that the only types that can be used are:

    byte, sbyte, short, ushort, int, uint, long or ulong

This does not include anything like System.Integer or System.Byte.