How can an int
be cast to an enum
in C#?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Below is a nice utility class for Enums
From a string:
From an int:
Update:
From number you can also
In my case, I needed to return the enum from a WCF service. I also needed a friendly name, not just the enum.ToString().
Here's my WCF Class.
Here's the Extension method that gets the Description from the Enum.
Implementation:
It can help you to convert any input data to user desired enum. Suppose you have an enum like below which by default int. Please add a Default value at first of your enum. Which is used at helpers medthod when there is no match found with input value.
N.B: Here I try to parse value into int, because enum is by default int If you define enum like this which is byte type.
You need to change parsing at helper method from
to
byte.TryParse(value.ToString(), out tempType)
I check my method for following inputs
sorry for my english
I think to get a complete answer, people have to know how enums work internally in .NET.
How stuff works
An enum in .NET is a structure that maps a set of values (fields) to a basic type (the default is
int
). However, you can actually choose the integral type that your enum maps to:In this case the enum is mapped to the
short
data type, which means it will be stored in memory as a short and will behave as a short when you cast and use it.If you look at it from a IL point of view, a (normal, int) enum looks like this:
What should get your attention here is that the
value__
is stored separately from the enum values. In the case of the enumFoo
above, the type ofvalue__
is int16. This basically means that you can store whatever you want in an enum, as long as the types match.At this point I'd like to point out that
System.Enum
is a value type, which basically means thatBarFlag
will take up 4 bytes in memory andFoo
will take up 2 -- e.g. the size of the underlying type (it's actually more complicated than that, but hey...).The answer
So, if you have an integer that you want to map to an enum, the runtime only has to do 2 things: copy the 4 bytes and name it something else (the name of the enum). Copying is implicit because the data is stored as value type - this basically means that if you use unmanaged code, you can simply interchange enums and integers without copying data.
To make it safe, I think it's a best practice to know that the underlying types are the same or implicitly convertible and to ensure the enum values exist (they aren't checked by default!).
To see how this works, try the following code:
Note that casting to
e2
also works! From the compiler perspective above this makes sense: thevalue__
field is simply filled with either 5 or 6 and whenConsole.WriteLine
callsToString()
, the name ofe1
is resolved while the name ofe2
is not.If that's not what you intended, use
Enum.IsDefined(typeof(MyEnum), 6)
to check if the value you are casting maps to a defined enum.Also note that I'm explicit about the underlying type of the enum, even though the compiler actually checks this. I'm doing this to ensure I don't run into any surprises down the road. To see these surprises in action, you can use the following code (actually I've seen this happen a lot in database code):
Alternatively, use an extension method instead of a one-liner:
Usage:
OR