4 min read

Enumerations

Crash Course

Introduction

In C#, types matter. Every variable, every value, and every expression has a type, and things must all align. In exchange, the compiler can do a whole lot to ensure things are being used correctly in your program, which saves you a lot of trouble.

Of course, the basic types that we’ve seen in C#–int, double, string, etc.–give us a lot of power, but they won’t capture every type imaginable.

The most powerful tool we have for defining new types is a class, which is something we’ll look at soon.

Right now, we’ll try our hand at defining new types by starting with enumerations . An enumeration is a type whose allowed values are specifically listed out, or enumerated.

There are a lot of concepts in the real world that follow this paradigm. Days of the week or months of the year are both good examples. Given the category, we can list all possible options.

Let’s suppose you’re designing a space RTS game, where players control ships, and each ship has a type that is either a frigate, a destroyer, a cruiser, or a battleship.

Given the tools we knew before this tutorial, you could imagine using the string type for this.

string shipType = "destroyer";

This has a couple of problems. There are a lot of valid strings that are not valid ship types:

string shipType = "hamburger";

Or what about this?

string shipType = "Destroyer";

Capitalization matters, and "Destroyer" == "destroyer" evaluates to false!

We could also have used an int. Perhaps we could assign each ship type a number.

int shipType = 0; // This is a frigate.

We could even make special variables to help us keep track of what number means what:

int frigate = 0;
int destroyer = 1;
int cruiser = 2;
int battleship = 3;

int shipType = frigate;

But we still have the problem of being able to assign this variable a value that makes no sense:

int shipType = 65;

Defining an Enumeration

Instead of either of these solutions, we can define a new enumeration type.

Before we look at how to define a new type, such as an enumeration, let’s talk about where you can place a new type definition.

If you’re using top-level statements, any type you place into your main file must come after everything else. If you define multiple types, their order among themselves doesn’t matter, but they have to come after all other statements:

using System;

DoSomething();

void DoSomething() { /* ... */ }

// <-- Type definitions, including enumerations, go here, after everything else.

If you’re using the traditional model, you will typically place new type definitions in the namespace, but outside of the Program class.

using System;

namespace ProjectNameHere
{
    // <-- Type definitions either go here...

    class Program
    {
        public static void Main(string[] args)
        {
            DoSomething();
        }

        static void DoSomething() { /* ... */ }
    }

    // <-- ... or here.
}

note

The third option is to place other type definitions in another file. It isn’t hard to make another .cs file and place other types in it by choosing File > New > File… in the menu, and then picking a reasonable template. The C# Class template is a good choice, though if you’re making an enumeration, you may want to strip out the class definition it drops in there.

Once we’ve figured out where to put our new enumeration definition, it is time to make it. An enumeration is defined by starting with the enum keyword, followed by a name for the new enumeration type, then placing all valid choices in a set of curly braces, separated by commas:

enum ShipType { Frigate, Destroyer, Cruiser, Battleship }

Some people will put each enumeration choice (called an enumeration member ) on its own line.

tip

The capitalization of enum names as well as enum member names is usually done with a style called UpperCamelCase or PascalCase. If you have a multi-word identifier, it is written out with a capital letter to start each word, and then otherwise, using lowercase letters. This makes it easy to visually separate one word from another. This is a guideline, not a hard rule, but it is the prevailing convention among C# programmers, and one you should try out for awhile before trying something else.

Using an Enumeration Type

Once you have defined the type in your program, you can make variables that use the enumeration type. This is done just like any other variable, only using your type’s name instead of one of the existing type names:

ShipType ship1;

To reference one of the specific enumeration members, you name the enumeration, followed by a dot, followed by the enumeration member name:

ShipType ship1 = ShipType.Destroyer;

The most typical thing to do with an enumeration type is to check to see if one value of the type equals another. For example:

ShipType ship = SomeMethodToPickAShipType();

if (ship == ShipType.Cruiser)
    Console.WriteLine("A cruiser has entered the system. Prepare to engage.");

You can use enumerations in switches as well, which is a popular pairing.