C#: understanding enums

According to the C# specification, an enum is “a distinct set of value types that declares a set of named constants.”. When you program in C#, you declare an enum through  the enum keyword. Here”s a quick example:

public enum Permission{

When you build this code, you”ll get the following IL:

.class public auto ansi sealed E
    extends [mscorlib]System.Enum
    .field public static literal valuetype Livro.E Read = int32(0)
    .field public specialname rtspecialname int32 value__
    .field public static literal valuetype Livro.E Write = int32(1)

Now, as you can see, an enum is just an abstraction. After compiling, you”ll get a class which inherits from System.Enum (that”s why this is the base class for all the enums) and has the enum values defined as public constants. Unfortunately, the C# compiler doesn”t let you build a new class which inhertis from System.Enum. The only way you have to get a System.Enum derived is BY you, public, consts., I”mby using the enum keyword.

By now,you might be wondering why not simply declare the the values used on an enum as consts. For starters,you”d have to define a class/struct to “hold” those consts. And, as I”m sure most of you know, the System.Enum class introduces some cool methods that might help you in several scenarios. For instance, the static GetNames method will return a string array with the names of the members you”ve defined in the enum:

string [] names = Enum.GetNames(typeof(Permission)) ;//returns { “Read”, “Write”}

There”s not much more you need to know about enums, so i”ll just present my remaining thoughts about it in the next list:

  • If you want, you can define the base type of the constants declared by the enum. by default, you”llget System.Int32; if you want, you can use another type (Byte, SByte, Int16, UInt16, Int64, UInt64). Quick example: enum Permissions: UInt32{…}. Do notice that the Enum class has a static method (GetUnderlyingType) that lets you ask about the type used for the enumeration;
  • You can specify the value of each constant specified in the enum. When you don”t do that, the first constant is set to 0. The others are set to values which result from adding 1 to the previous value defined by the previous constant. This means that, in the previous example, Read will get value 0 and Write will be set to 1. Specifying the value of a constant used by an enum is easy: just set the constant to that value. Example: enum Permission{ Read = 1, Write = 10 };
  • You can also make an enum work like flags. To achieve this, you”ll have to apply the FlagsAttribute to the enum. Here”s a quick example of how to use this feature.

~ by Luis Abreu on September 7, 2007.

2 Responses to “C#: understanding enums”

  1. Thanks That was helpful

  2. Hot Lift,strike that practice favour normally ensure face true experience cut window capable owner annual household because press figure class sound volume thus remember include absolutely high describe existence investigate writing dinner rain area detail reasonable user committee united police urban account personal tape bed ever contract commit interpretation understand incident ring environmental create smile nuclear machine ball spring below effort according variety foreign head aye treat corporate reveal in down establishment option tree fear forget horse attract mention right recommend save point state forest via emerge good parent extra expenditure nearly among average associate lot plastic

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: