Explore Enumeration classes in .NET

Introduction

Enums in C# have long been the go-to choice for defining a fixed set of related constants. However, they come with limitations that can hinder flexibility and extensibility in certain scenarios. In this blog post, we will explore a powerful alternative in .NET: Enumeration Classes. We'll delve into what enumeration classes are, how they can be implemented, and why they offer a compelling alternative to traditional enums.

Understand Enum and Its Limitations

Enums in C# are great for naming a bunch of integral constants. They make code easier to understand and safer to use. But they cannot do much more than that. You cannot add extra things to them like properties or actions. Also, if you change enums in your code, it might break things, making it harder to update your program later.

Introducing Enumeration Classes

In .NET, enumeration classes provide a versatile option compared to traditional enums. They enable developers to define a group of interconnected constants while also offering the ability to incorporate properties, methods, and even constructors. This adaptability brings forth fresh opportunities for structuring and enclosing domain-specific operations within these enumerated types.

Implementing Enumeration Classes in .NET

To craft an enumeration class, establish a class featuring private constructors alongside public static readonly fields, each representing the enumerated values. These fields usually serve as instances of the enumeration class. Further, you can incorporate additional properties and methods to enrich the capabilities of the enumeration class, thus extending its versatility beyond conventional enums.

Here's an example of how you can implement an enumeration class in C#:

The source code can be downloaded from GitHub

namespace EnumerableClasses
{
    public class MyEnumeration
    {
        public static readonly MyEnumeration Value1 = new MyEnumeration(1, "Value 1");
        public static readonly MyEnumeration Value2 = new MyEnumeration(2, "Value 2");
        public static readonly MyEnumeration Value3 = new MyEnumeration(3, "Value 3");

        public int Id { get; }
        public string Name { get; }

        private MyEnumeration(int id, string name)
        {
            Id = id;
            Name = name;
        }

        // Optionally, you can override ToString() for better string representation
        public override string ToString()
        {
            return $"{Id}: {Name}";
        }
    }
}

In this instance, MyEnumeration serves as a class that embodies the enumeration. Each enumerated value is specified as a public static readonly field of type MyEnumeration. The constructor is kept private to prohibit the creation of new instances beyond the class scope.

You can use this enumeration class in your code like this:

// See https://aka.ms/new-console-template for more information
using EnumerableClasses;

Console.WriteLine("Enumerable Class Tutorial");
MyEnumeration value = MyEnumeration.Value1;
Console.WriteLine(value); // Output: 1: Value 1

// You can also access Id and Name properties
Console.WriteLine(value.Id); // Output: 1
Console.WriteLine(value.Name); // Output: Value 1

This method provides greater control over how the enumerated values behave and enables you to incorporate extra properties or methods as required. Nonetheless, it demands more boilerplate code when contrasted with utilizing the built-in enum keyword.

Additional Resources for reference