.NET Type System, Part I - Enumerations
(Page 6 of 6 )
Enumerations are not a new construction to programming languages. In .NET and C# they have been designed as Object-Oriented Construction, and this gives developers a lot of new features that were not available, for example, in C++.
An Enumeration is a set of named-values. In other words, it's a set of numerical values associated with a name for each value. You define Enumerations in C# like the following example, a Jobs Enumeration:
enum Jobs
{
Programmer, //Implicitly assigned a value of 0
Administrator, //Implicitly assigned a value of 1
Designer, //Implicitly assigned a value of 2
Architect, //Implicitly assigned a value of 3
Manager //Implicitly assigned a value of 4
}
We use the keyword enum followed by the Enumeration identifier. Before we talk more about Enumerations you should know that every time you define an Enumeration, it inherits from the base-class System.Enum, which in turn inherits from System.ValueType, which in turn inherits from System.Object.
As we said before, an Enumeration is a set of names (or symbols) associated with a value. In the Jobs enumeration, as you can see, my comments //Implicitly assigned a value of 0 and so on. The values are not explicit, but will be set by the C# Compiler. Each Enumeration has an underlying type, which represents the values that will be assigned to the symbols. By default it's int, but you can use byte, sbyte, uint, ulong, long, short and ushort as the underlying type, as the following example:
enum Jobs: byte
{
Programmer, //Implicitly assigned a value of 0
Administrator, //Implicitly assigned a value of 1
Designer, //Implicitly assigned a value of 2
Architect, //Implicitly assigned a value of 3
Manager //Implicitly assigned a value of 4
}
You can explicitly define the values:
enum Jobs: byte
{
Programmer = 1,
Administrator = 2,
Designer = 3,
Architect = 4,
Manager = 5
}
You can define the values in what order you like:
enum Jobs: byte
{
Programmer = 5,
Administrator = 4,
Designer = 1,
Architect = 2,
Manager = 3
}
You can define a value for the first symbol and the other symbols' values will be implicitly added:
enum Jobs: byte
{
Programmer = 1,
Administrator, // here is 2
Designer, // here is 3
Architect, // here is 4
Manager // here is 5
}
As you can see, Enumerations are a very flexible Object-Oriented construction. They make developing applications easier because you don't have to remember the types of jobs you have in your application and their values. For example, you will just write Jobs.Programmer instead of writing the value 1 (maybe as an argument to a method that has a paramter of type Jobs). Besides that, Enumerations in .NET are Strongly-Typed, which means that you can't pass a Jobs value to a method's parameter that expects a Titles Enumerations value.
Enumerations are a Value-Type like Structures. Under the hood it looks like a structure; when the C# compiler compiles the Jobs Enumeration it makes all the symbols and their associated values into constant members of a structure like the following pseudo code:
struct Jobs: System.Enum
{
public const Jobs Programmer = (Jobs)1;
public const Jobs Administrator = (Jobs) 2;
public const Jobs Designer = (Jobs) 3;
public const Jobs Architect = (Jobs) 4;
public const Jobs Manager = (Jobs) 5;
}
This pseudo code should clear the internal working of an enumeration, it's a structure that defines constant values for the symbols that you want to define. All the fields have been declared as public const, which means that in the compile-time the compiler will replace any references to Jobs.Programmer, Jobs.Administrator and so on by their numerical value. Also note that the produced compiled file (the assembly) is full of information about the Types you defined and about Enumerations Types. The assembly will contain information about each symbol and its associated value, so we can use casting operations to get the underlying value of a given symbol as with the following:
namespace Structures
{
public class Test
{
static void Main()
{
Console.WriteLine(Convert.ToString((byte)Jobs.Administrator));
Console.ReadLine();
}
}
enum Jobs: byte
{
Programmer = 1,
Administrator, // here is 2
Designer, // here is 3
Architect, // here is 4
Manager // here is 5
}
}
The output will be:
2
We have used the cast operator (type), in our example (byte) to cast to the underlying type of the Jobs Enumeration, to cast the Symbol Administrator of the Jobs Enumeration to its numerical value. The Convert.ToString() method returns a string representation, and here we pass the expression that will return a byte value of 2 (which represents the Jobs.Administrator value). I think it will be interesting at this point to take a look at the MSIL as we did with structures. Create a Enum.cs file, copy only the Jobs Enumerations to this file, and compile it using the following command
c:\csc /t:library Enum.cs
Now let's open it using ILDASM tool using the following command:
c:\ ildasm Enum.dll
The ILDASM tool will load the class library and you will get the same result as the following:

As you can see the Jobs Enumeration contains constant values and when you click on one of them you will get a window with its value:

We have assigned the value 1 to the Jobs.Programmer symbol, and as you can see here it has 0x01 value (means 1). I have told you programming in C# is fun, especially with the ILDASM tool, so you can understand what happens under the hood.
In the second part of this article I will discuss Reference-Types and you will learn about Classes, Arrays, Strings and Boxing and Unboxing operations.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |