Within C# I use enumerations a lot. An enumeration is a distinct type which contains a collection of named constants. It can be a lot of underlying types (byte, sbyte, short, ushort, int, uint, long, or ulong) but i usually see the default which is an int (Int32). You can however easily create bugs when working with enums. Read on if you want to find out more.

To explain the problem we need a nice and clean enum

public enum Days {
   Sat,
   Sun, 
   Mon,
   Tue,
   Wed,
   Thu,
   Fri
};

This looks like a good example which I almost copied from an MSDN page somewhere. There are however a couple of things ‘wrong’ with it.

‘Unexpected’ default behaviour (1)
So what do I mean with unexpected default behavior suppose we have the following class

 public class DoSomething
 {
        public Days SelectedDay { get; set; }
 }

We now create an instance of this class with var x = new DoSomething(). The value of the SelectedDay field now is Sat. Suppose you want to allow a user to selected some information you also want to validate that they didn’t forget inputting it. But how can we write this code because the enum value “Sat” is a correct one for this enum. So then what should we do, we add a value which I usually call “Unknown”. No we can write the validate method correctly.

    public enum Days
    {
        Unknown,
        Sat,
        Sun,
        Mon,
        Tue,
        Wed,
        Thu,
        Fri
    };

    public class DoSomething
    {
        public Days SelectedDay { get; set; }

        public bool IsValid()
        {
            if (SelectedDay != Days.Unknown)
                return true;

            return false;
        }
    }

Does this mean all our problems are over? No, sadly not. Let’s see the second reason below

‘Unexpected’ default behaviour (2)

After our first experiment we have another developer changing the enum just slightly. like you can see below.

public enum Days
{
    Sat,
    Sun,
    Mon,
    Tue,
    Wed,
    Thu,
    Fri,
    Unknown,
};

This causes our validation logic to fail again because the default value for this enum changed from “Unknown” to “Sat” again. Damn.. this stuff is hard. The reason for that is that underlying type of our enum is an integer. So it needs to attach numbers to the constants. The first enum member gets the value “zero”. The following “one” and so on. But whats the default value for an integer? it’s also “zero” right? So this way our default value changes every time the enumeration order is changed. But there is a solution for this. You do you own numbering in an enum:

public enum Days
    {
        Sat =1,
        Sun =2,
        Mon =3,
        Tue =4,
        Wed =5,
        Thu =6,
        Fri =7,
        Unknown =0, //this is the default value
    };

So after all our attemps we have finally fixed it. We have added a default value and assigned integer values to each member. So we’re done right? Well most of the time…

Storing enumerations in a database or a file
Whats wrong with doing this? Well nothing at all you just have to remember ‘one more thing’. Enumerations are stored in the database/file as an integer value. Which get the value you just added (or forgotten initially). So suppose the Days enum needs to changed or reordered? You then still need to update the data in the database or the file so that it matches with the new enumeration values.

There a couple of default gotcha’s when using enumerations. I hope i’ve given you a list of things to watch our for. So please don’t make me see enums without values or defaults again.

Leave a Reply

Your email address will not be published. Required fields are marked *