The Crash Course

  • Enumerations are a way of defining your own type of variable so that it has specific values.
  • Enumerations are defined like this: public enum DaysOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
  • Enumerations should be defined outside of any classes that you have.
  • The fact that an enumeration can only take on the values that you chose, means that you can't possibly end up with bad data that is meaningless.
  • You can assign your own numeric values to the items in an enumeration: public enum DaysOfWeek { Sunday = 5, Monday = 6, Tuesday = 7, Wednesday = 8, Thursday = 9, Friday = 10, Saturday = 11 };


We've been going at a pretty fast pace. There's been lots to learn in every tutorial. This one will be a bit of a break, though, as enumerations a pretty simple and easy.

Enumerations (or enumeration types, or enums for short) are a cool way to define your own type of variable (the first of many ways we'll see to create your own).

The word "enumeration" comes from the word "enumerate", which means "to count off, one after the other", which is basically what we're going to be doing. We're going to start off with the basics of using enumerations, but then we'll come back and discuss why enumerations are so valuable. We'll do this so that it makes more sense what an enumeration is before trying to explain how helpful they are. Then we'll go back to some of the details of using an enumeration before the end.

The Basics of Enumerations

Let me start off with an example of when enumerations may be helpful. Let's say you are keeping track of something that has a known, small, specific set of possible values that you can use—for instance, the days in the week. You know, Sunday, Monday, Tuesday…. One way you could do this is to define Sunday to be 1, Monday to be 2, Tuesday to be 3, and so on.

So your code could look something like this:

int dayOfWeek = 3;
if(dayOfWeek == 3)
    // Do something here because it is Tuesday.

Or better yet, for the sake of readability, you might create a variable to store each of these days:

int sunday = 1;
int monday = 2;
int tuesday = 3;
int wednesday = 4;
int thursday = 5;
int friday = 6;
int saturday = 7;
int dayOfWeek = tuesday;
if(dayOfWeek == tuesday)
    // Do something here because it is Tuesday.

This kind of situation, though, is exactly what enumerations are for. You create an enumeration, and list the possible values it can have.

To create an enumeration, you make it outside of any classes that you have (we'll talk more about classes in a few tutorials—right now, you only have the Program class in your C# file, which Visual C# Express generated for you). You use the enum keyword, give your enumeration a name, and then, inside of curly braces ('{' and '}'), you list the values that your enumeration can have:

public enum DaysOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

Remember, this goes outside of other classes, so your whole program will look something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.Net;
namespace EnumerationTutorial
    // You define enumerations outside of other classes.
    public enum DaysOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
    public class Program
        static void Main(string[] args)

Now, inside of your Main method, you can create variables that have your new DaysOfWeek type, instead of int or some other type. Creating a variable with your new type works just like any other variable:

DaysOfWeek today;

To assign it a value, you will use the '.' operator (pronounced "dot operator"). We'll discuss the '.' operator more later, but for now, what you need to know is that it is used for "member access". This means that you use the '.' operator to use something that is a part of something else. The values in our enumeration are a part of the enumeration, so we use this operator to use them.

today = DaysOfWeek.Tuesday;

This works like any other variable, even for things like comparing in an if-statement:

DaysOfWeek today = DaysOfWeek.Sunday;
if(today == DaysOfWeek.Sunday)
    // ...  

Why Enumerations are Useful

There's a very good reason to use enumerations. Let's go back to where we first started this tutorial—doing days of the week with the int type. Remember how we had defined dayOfWeek = 3; which meant Tuesday? What if someone put dayOfWeek = 17;? As far as the computer knows, this should be valid. After all, dayOfWeek is just an int variable. Why not allow it to be 17? But as far as what we're actually trying to do, 17 doesn't make any sense. We are only using 1 through 7.

Enumerations force the computer (and programmers) to use only specific values, which you have defined. It prevents tons of errors, and makes your code more readable (for example, dayOfWeek = DaysOfWeek.Sunday is immediately clear what it means, while dayOfWeek = 1 is not).

In addition, enumerations allow us to use the IntelliSense that Visual C# Express (or Visual Studio) provides, which is another nice little bonus.

Some More Details of Enumerations

Before moving on, it is worth looking at one more detail of enumerations. Under the surface, C# is simply wrapping our enumeration around ints. So they are, at their core, just numbers. (As I just mentioned, though, it's also doing a lot of work to make sure that only the right numbers are being used.) When you create an enumeration, it starts off giving them values, starting with 0, and going up by one for each new number. So in the enumeration that we created, Sunday has a value of 0, Monday has a value of 1, and so on.

This means that you can cast to and from an int or other number like this:

int dayAsInt = (int)DaysOfWeek.Sunday;
DaysOfWeek today = (DaysOfWeek)dayAsInt;
DaysOfWeek tomorrow = (DaysOfWeek)(dayAsInt + 1);

(Note that an explicit cast is required to go either direction—from the enum type to an int and from an int to the enum type.)

When you create an enumeration, you can additionally assign it your own numeric values, besides the default, if that is of any value to you (usually not, but occasionally it is):

public enum DaysOfWeek { Sunday = 5, Monday = 6, Tuesday = 7, Wednesday = 8, Thursday = 9, Friday = 10, Saturday = 11 };

What's Next?

Enumerations are only the first of many ways that we'll see to make your own type of variables. There are lots of cool ways you can use enumerations, like the type of terrain you are using for a tile in your game (public enum TerrainType { Mountain, Water, Desert, Grassland, Forest };) or the suits of a deck of playing cards (public enum Suit { Clubs, Diamonds, Spades, Hearts };).

We've now covered many of the very basic topics in C#, and we're ready to start getting into some of the more advanced features that will really let us create powerful and awesome programs. Our next stop is an important one: methods.