C# Enumeration Cheat Sheet
Emumeration in C# is a great technique to use whilst coding - e.g. when dealing with flags in a database you may have lots of different numeric flags, such as "1" for "accepted", "2" for "declined" and so on. Using enumerators you can just use somthing like "Status.Declined" instead of trying to remember what the numbers mean. It certainly beats using static final int all the time!Overriding Default Values and Underlying Types
By default, all enumerations have an underlying type of int and start at 0.
enum Days {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday
}
Here, the value for Monday is 0, Tuesday is 1 and so on.
We can override the default values really easily - say we wanted to have Monday's value as 1, Tuesday 2, Wednesday 4 and so on, we'd do this:
enum Days {
Monday=1,
Tuesday=2,
Wednesday=4,
Thursday=8,
Friday=16
}
Of course we might not want to use int as the type, for example we might want to use long so we can store some larger numbers in there for whatever reason.
enum Days : long {
Monday=2147483648,
Tuesday=2147483649,
Wednesday=2147483650,
Thursday=2147483651,
Friday=2147483652
}
Getting the value of an enum member
To get a value out of an enum member (e.g. if we wanted to know what number Wednesday was) you'll need to cast it back first.
// WednesdayValue = 2147483650
long WednesdayValue = (long) Days.Wednesday;
Textual descriptions for enum members
Sometimes its cool to have a nice, human understandable description for each of your enum members. For the "Days" example I've used above its not really an issue as we can just use the standard .ToString() method.
// Displays "Monday"
Console.Write(Days.Monday.ToString());
But what if we had some status flags? Say we had the following enumeration:
enum Status {
Waiting=1,
Shipped=2
}
Ok thats fine - we can say in our application "Waiting" or "Shipped" using the .ToString() method, but if we want we can give it better descriptions we simply provide a "Description" attribute for each member, then use the function below to return it as a string.
// Ideally, this code would go in some "Utility" class
// hence the use of the static keyword.
public static String GetEnumDesc(Enum e) {
System.Reflection.FieldInfo EnumInfo = e.GetType().GetField(e.ToString());
System.ComponentModel.DescriptionAttribute[] EnumAttributes =
(System.ComponentModel.DescriptionAttribute[])
EnumInfo.GetCustomAttributes
(typeof(System.ComponentModel.DescriptionAttribute), false);
if (EnumAttributes.Length > 0) {
return EnumAttributes[0].Description;
}
return e.ToString();
}
enum Status {
[Description("Item is waiting for dispatch.")]
Waiting=1,
[Description("Item has been shipped.")]
Shipped=2
}
.
.
.
// Displays "Item is waiting for dispatch."
Console.Write(GetEnumDesc(Status.Waiting));
So there you go, the enumerator cheat sheet. Please feel free to leave any comments!
Back
29.05.2006.
Pretty sure this works fine with 1.1!
Matt
12.11.2006
the source code is without cr+lf
jei
20.11.2006
not clear
mansoor
05.12.2006
Cool
Sean
02.02.2007
cool working..
vel
23.02.2007
I want to use enum values as inexes into a string array. But, when I specify the emum constant as an index value, C# complains that it is not an integer! Even though I explicitly typed the num values as integers. How does this work. It so EASY in vb to make this work - what is wrong with this C#?
enum K : int { RE=0, Salutation, Thanks, Regrets };
string[] sLtrType = new string[11];
sLtrType[K.RE] = "This is a test"; // this fails!
Richard Ainsley
15.03.2007
try this
sLtrType[(int)K.RE] = "This is a test";
bh0k44l
16.03.2007
GR8 Work
Amrinder Singh Ahlamadi
01.05.2007
U R GREAT !!!
Ofer
10.05.2007
You forgot to the code for creating the attribute class:
[AttributeUsage(AttributeTargets.Field)]
public class Description : Attribute
{
string descript;
public Description(string desc)
{
this.descript = desc;
}
}
sprocketonline
10.05.2007
Hi,
Thank you for this. Is that only possible with the .Net 2.0? If so, do you know how to do it with .Net 1.1?
Regards,