Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Friday, May 25, 2012
Cast int to Enum in C#
What's a quick and easy way to cast an int to an enum in c#?
public static class EnumHelper { public static int[] ToIntArray<T>(T[] value) { int[] result = new int[value.Length]; for (int i = 0; i < value.Length; i++) result[i] = Convert.ToInt32(value[i]); return result; }
public static T[] FromIntArray<T>(int[] value) { T[] result = new T[value.Length]; for (int i = 0; i < value.Length; i++) result[i] = (T)Enum.ToObject(typeof(T),value[i]); return result; }
internal static T Parse<T>(string value, T defaultValue) { if (Enum.IsDefined(typeof(T), value)) return (T) Enum.Parse(typeof (T), value);
int num; if(int.TryParse(value,out num)) { if (Enum.IsDefined(typeof(T), num)) return (T)Enum.ToObject(typeof(T), num); }
If you have an integer that acts as a bitmask and could represent one or more values in a [Flags] enumeration, you can use this code to parse the individual flag values into a list:
for (var flagIterator = 0x1; flagIterator <= 0x80000000; flagIterator <<= 1) { // Check to see if the current flag exists in the bit mask if ((intValue & flagIterator) != 0) { // If the current flag exists in the enumeration, then we can add that value to the list // if the enumeration has that flag defined if (Enum.IsDefined(typeof(MyEnum), flagIterator)) ListOfEnumValues.Add((MyEnum)flagIterator); } }
If you're ready for the 4.0 .Net Framework, there's a new Enum.TryParse() function that's very useful and plays well with the [Flags] attribute. See http://msdn.microsoft.com/en-us/library/dd783499.aspx
From a string:
ReplyDeleteYourEnum foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString);
From an int:
YourEnum foo = (YourEnum)yourInt;
Update :
From number you can also:
YourEnum foo = Enum.ToObject(typeof(YourEnum) , yourInt);
Just cast it:
ReplyDeleteMyEnum e = (MyEnum)3;
You can check if it's in range using Enum.IsDefined:
if (Enum.IsDefined(typeof(MyEnum), 3)) { ... }
int one = 1;
ReplyDeleteMyEnum e = (MyEnum)one;
For those people stopping by, who wants to use as an extension method.
ReplyDeletepublic static T ToEnum<T>(this string enumString)
{
return (T) Enum.Parse(typeof (T), enumString);
}
Usage
Color colorEnum = "Red".ToEnum<Color>();
OR
string color = "Red";
var colorEnum = color.ToEnum<Color>();
Below is a nice utility class for Enums
ReplyDeletepublic static class EnumHelper
{
public static int[] ToIntArray<T>(T[] value)
{
int[] result = new int[value.Length];
for (int i = 0; i < value.Length; i++)
result[i] = Convert.ToInt32(value[i]);
return result;
}
public static T[] FromIntArray<T>(int[] value)
{
T[] result = new T[value.Length];
for (int i = 0; i < value.Length; i++)
result[i] = (T)Enum.ToObject(typeof(T),value[i]);
return result;
}
internal static T Parse<T>(string value, T defaultValue)
{
if (Enum.IsDefined(typeof(T), value))
return (T) Enum.Parse(typeof (T), value);
int num;
if(int.TryParse(value,out num))
{
if (Enum.IsDefined(typeof(T), num))
return (T)Enum.ToObject(typeof(T), num);
}
return defaultValue;
}
}
Sometimes you have and object to the MyEnum type. Like
ReplyDeletevar MyEnumType = typeof(MyEnumType);
then:
Enum.ToObject(typeof(MyEnum), 3)
If you have an integer that acts as a bitmask and could represent one or more values in a [Flags] enumeration, you can use this code to parse the individual flag values into a list:
ReplyDeletefor (var flagIterator = 0x1; flagIterator <= 0x80000000; flagIterator <<= 1)
{
// Check to see if the current flag exists in the bit mask
if ((intValue & flagIterator) != 0)
{
// If the current flag exists in the enumeration, then we can add that value to the list
// if the enumeration has that flag defined
if (Enum.IsDefined(typeof(MyEnum), flagIterator))
ListOfEnumValues.Add((MyEnum)flagIterator);
}
}
I am using this piece of code to cast int to my enum:
ReplyDeleteif (typeof(YourEnum).IsEnumDefined(valueToCast)) return (YourEnum)valueToCast;
else { //handle it here, if its not defined }
I find it the best solution.
If you're ready for the 4.0 .Net Framework, there's a new Enum.TryParse() function that's very useful and plays well with the [Flags] attribute. See http://msdn.microsoft.com/en-us/library/dd783499.aspx
ReplyDeleteYou can use TryParse():
ReplyDeleteColor mycolor;
if(Enum.TryParse<Color>("Red", out mycolor)){
//Parse Color Successfully
}