Enumerations

This is the first of a series of posts about C#. Since I can’t really think of tiny apps to put stuff that I’ve learned to practice, the least I can do is blog about it, hopefully to boost my memory on it. So let’s get on with it..

In short, an enumeration is a user-defined interger type. Let’s say you want to define the colors of a traffic light. Defining them with strings has it’s associated overheads. The most memory-efficient way is to use integers to represent the colors: say, 1 for red, 2 for yellow, 3 for green. Problem is, the numbes 1, 2 and 3 aren’t really self-explaining. One might mistake 3 for red for example. The next best way is to use some constants:


const int RED = 1
const int YELLOW = 2
const int GREEN = 3

Hence, you get something that’s more memory efficient than strings, and meaninful too. But this method isn’t fool-proof either. Take for example a method that does something depending on the traffic light’s color.


private void doSomething(int trafficLightColor)
{
	// some switch statements..
}

The method will except an int type, since the traffic light colors are defined as constant integers. With such an implementation, any integer can be passed into the method, like 100 or -23, which can cause unexepceted run-time bugs. Using an enumeration will solve such a problem entirely. We define the eumeration like this:


public enum TrafficLightColor : byte
{
	Red = 1, Yellow, Green
}

Red will be represented with the “integer” 1, yellow: 2, and green 3. Since we have only 3 possible values, the byte type will be enough. Now, the method doSomething() can be rewritten as follows:


public void doSomething(TrafficLightColor tlc)
{
	switch (tlc)
	{
		case TrafficLightColor.Red:
		case TrafficLightColor.Yellow:
		case TrafficLightColor.Green:
			// do somes stuff...
			break;
	}
}

// can be called with something like:
doSomething(TrafficLightColor.Yellow);

Look closely at the parameters for doSomething(). It accepts an enumeration type: TrafficLightColor. That effectively limits the range of values accepted by the method. Through the usage of enumeration-type.name syntax, codes are self-explaining. What’s more, thanks to IntelliSense, Visual Studio will pop up a list of names when you type in the enumeration type.

Enumerations are great for “internal representations of data”, internal meaning in control and entity classes. User input will need to be “converted” to enum data types. In other words, enums are used by programmers as an efficient way (using integers, not strings) of representing data with limited ranges. For example, months (Jan - Dec), seasons in a year and the like. Speaking of seasons, C# allows more than one enumration literal to be assigned the same underlying value:


enum Season { Spring, Summer, Fall, Autumn = Fall, Winter }

So now Season.Fall and Autumn.Fall will mean the same thing. However, I think such a practice isn’t good as it might lead to confusion. Next up: a brief round-up of preprocessor directives, comments (or rather, documentation) and perhaps some other stuff.

Tags: ,

Leave a Reply