C# Enum Array Example

Enum array. An enum array has many uses. It can be accessed with enum values. This technique is ideal for some kinds of data such as numeric data. It provides another way to keep track of values or statistics.

Info: This example shows how to index an array of values with enum keys. It shows an array with enhanced syntax.

Example. First, enum values are actually integral types, meaning they are usually Int32 values. Like any integer value, you can access an array with their values. Enum values are ordered starting with zero, based on their textual order.


Here: In the following example, the first enum named constant in MessageType is zero, and the second is one.

C# program that uses enum indexes

using System;

/// <summary>
/// Enum values used to index array.
/// </summary>
enum MessageType

/// <summary>
/// Contains array of elements indexed by enums.
/// </summary>
static class Message
    /// <summary>
    /// Contains one element per enum.
    /// </summary>
    public static int[] _array = new int[(int)MessageType.Max];

class Program
    static void Main()
	// Assign an element using enum index.
	Message._array[(int)MessageType.Startup] = 3;

	// Assign an element.
	Message._array[(int)MessageType.Error] = -100;

	// Increment an element using enum index.

	// Decrement an element using enum index.

	// Preincrement and assign an element.
	int value = ++Message._array[(int)MessageType.Shutdown];

	// Loop through enums.
	for (MessageType type = MessageType.Startup; type < MessageType.Max; type++)
	    Console.Write(' ');


Startup 3
Shutdown 1
Reload 0
Refresh 0
Sleep 0
Error -100

First, we see the MessageType enum, which is a series of int values you can access with strongly-typed named constants. We also see a class containing the array we will access with those constants. The third part is the Main method.

Tip: MessageType contains seven named constants. It is usually best to start with a None constant and end with a Length constant.

Message class. The class shown next is the Message class and it contains an array. The array is initialized to contain the number of elements that is equal to the count of named constants in the MessageType enum.

Tip: This makes it easy to use the named constants to access parts of this array.

Note: The array is public and static. It is a global variable array. It can be used throughout the program.

The statements in the method's body assign, increment, decrement, and preincrement elements in the array using the MessageType enumeration's named constants. This changes the values in the public static array.

And: The enumeration values are treated as integers by using explicit casts. MessageType.Startup is 0. MessageType.Error is 5.

Finally: The example shows how to loop through enumerated values. The for-loop here goes through each enum except the last one.

Access routines. This code basically shows how to use a global variable array in a reasonable way. When using global variables, you should use accessor routines, which provide a level of abstraction on your usage of the globals.

The example does not show any access routines, but you could easily implement some on the Message class. It would be best to even remove the public modifier from the array. You can find tips on access routines in Code Complete (page 340).


Exceptions. Our code in this article is vulnerable to exceptions during runtime. You can use access routines to mitigate this possibility. For critical variables that must be statics, it is still best to use separate variables at the type level.

Tip: Because the array lookup must occur before getting the value, the code here is slightly slower in many cases as well.

But: On the other hand, it forces the variable to stored together, which will provide better locality of reference.

Uses. The array element indexing shown in the example is used in many important programs. It is particularly useful for collecting runtime statistics for release mode debugging support.

Tip: You can track what methods are executed and how often in your ASP.NET website.

Summary. We accessed array elements using enumerated types. We looked at an example of code that looks up elements this way and then saw its output. We next discussed some problems with this code and some of its benefits.