Thursday 22 February 2007

C# Enum, enumerated types

C# Enumerated type info

Declaration:
enum MyType
{
One=1,
Two=2
};



Assignment:

int myVar = (int) MyType.One;
MyType myVar2 = MyType.Two;


Getting name of value:
string enumName = MyType.One.ToString();


Getting all values as string:
string[] enumNames = Enum.GetNames(typeof(MyType));


Getting an Enum value from a string:
MyType myVar = (MyType)Enum.Parse(typeof(MyType),"One",true)