Microsoft identifies three ways of initializing values for enums, all of with which you should be familiar:

  1. Specify none of the values (implicit initialization):
    enum Meat { Chicken, Beef, Lamb }
  2. Specify all of the values (explicit initialization):

    enum Meat { Chicken = 3, Beef = 14, Lamb = 18 }
  3. Specify the starting value (part of what I’ll call partial initialization):

    enum Meat { Chicken = 1, Beef, Lamb }

It is this latter case that I want to expand on (but only a little bit).

First off, Microsoft doesn’t really recommend having a starting value for an enum without explicitly providing a default value:

In this enumeration, the sequence of elements is forced to start from 1 instead of 0. However, including a constant that has the value of 0 is recommended. For more information, see Enumeration Types (C# Programming Guide).

This is based on how enumerations work under the covers. Unless a member is explicitly set to use the value of 0, the default value is the first one in the list. So, in the previous example, the default value for the enum will be Meat.Chicken. However, if I define the enum like this:

enum Meat { Chicken = 1, Beef = 0, Lamb = 18 }

then the default value will be Meat.Beef. So far, this should be nothing new to you.

Here’s something that might be new, though: you can mix-and-match implicit and explicit initialization anywhere in the enum.

Try to imagine what will happen in this case:

enum Shape { Square = 12, Circle = 17, Triangle, Trapezoid = 22 }

Your first assumption would be correct: Shape.Triangle is 18. Seems innocent enough, right? But note what happens when you add a value like this:

enum Shape { Square = 12, Circle = 17, Hexagon = 18, Triangle, Trapezoid = 22 }

Well, now Shape.Triangle is 19! If you aren’t paying close attention when making such a change, then you’re going to break data.

Let’s go ahead and add another shape:

enum Shape { Square = 12, Circle = 17, Hexagon = 18, Triangle, Dodecahedron = 19,
    Trapezoid = 22 }

This is bad news also. You might think that the compiler would figure out a way to resolve this. However, it really screws things up. Check the value of Shape.Triangle now: again 19!

Looking at all these cases, there’s a simple rule of thumb you can follow when it comes to partial initialization: don’t do it. Either specify values for all the members in your enum, or none of them. Mixing and matching is a recipe for inadvertently introducing bugs into your application.