Enums - mcbride-clint/DeveloperCurriculum GitHub Wiki

Enumeration types in C# can be a very useful tool in making your code more readable and maintainable. Enums create named integer constants. This allow you to group associated constants into a reusable object that can add additional context to your code.


// enums that do not have set integer values underneath
public enum LocationType 
{
  House,
  Apartment,
  Business
}
// Note: in the background integer values are assigned automatically (0, 1, 2)

// enums with an underlying value that can be used for comparisons
public enum PositionLevel
{
  Associate = 1,
  Senior = 2,
  Manager = 3
}

Enums can be used in place of normal integers when passing as a parameter.


// Using integer codes
var employee = new Employee(2);

// Using the enums makes the code rely less on "magic" numbers to work and readable
var employee = new Employee(PositionLevel.Senior);

Enums can usually be easily inserted and retrieved from a database. The value stored will be the underlying integer value so using an explicitly set underlying value will help to maintain consistency.

Non-Integer Enums

Once a developer begins using enums, it can quickly become apparent that they need some additional functionality such as a code for an enum so they can have a description/code pair.

They may try incorrectly:


public enum Status
{
  AwaitingShipment = "A",
  Shipped = "S",
  Received = "R"  
}

This is not supported in C#, but due to the customizable nature of C# a similar effect can be achieved using static classes and static constants.


public static class Status
{
  public static string AwaitingShipment = "A";
  public static string Shipped = "S";
  public static string Received  = "R";
}

...
// Then this can be used
var itemStatus = Status.Shipped; // actually Stores an "S"
...

The down side of this method is strongly typed parameters. A parameter looking to accept a Status has to accept a string.

This could even be further using classes and static instances of that class.


public static class Status
{
  public string Code { get; }
  public string Description { get; }

  public Status (string code, string description){
    Code = code;
    Description = description;
  }

  public static Status AwaitingShipment = new Status("A", "AwaitingShipment");
  public static Status Shipped = new Status("S", "Shipped ");
  public static Status Received = new Status("R", "Received  ");
}

...
// Then this can be used
var itemStatus = Status.Shipped; // actually a reference to the static Shipped class
Console.WriteLine(itemStatus.Code); // Prints "S"
Console.WriteLine(itemStatus.Description ); // Prints "Shipped"
...

// A function's parameters can now be typed to only accept statuses
public void ShowStatus(Status status)
{
  ...
}

Using classes has the additional advantage of allowing you to add additional properties to each object later if needed. The disadvantage of this technique is that it can be more difficult to directly insert and retrieve this data from a database. Additional mapping would be required to transfer this data out of the data layer of your application.

Repo Programming Examples:

See Also