Ardalis.SmartEnum for C# (.NET): Complete Guide with Examples
\n\n1. What It Is and Why You Need It
\n\n Ardalis.SmartEnum is a library for C# (.NET) that solves one of the most common problems in development: the inconvenience of working with enumerations (enum). Built-in C# enums are essentially a set of integer constants. They cannot contain logic, additional fields (e.g., description, currency code, color), or methods. This leads to code bloat with switch or if-else statements, violating SOLID principles (specifically the Open/Closed Principle) and making the code less maintainable.\n
\n SmartEnum offers an alternative: Smart Enums. These are classes that inherit from the base class SmartEnum<TEnum, TValue>. Each value of such an enumeration is a static singleton field. This allows you to add any properties (Name, Value are mandatory, plus any custom ones), methods, and even implement interfaces. This transforms an enumeration from a simple set of numbers into a full-fledged object with behavior.\n
\n Why is this needed? First, for type safety. You cannot accidentally assign a value to a variable that is not defined in the enumeration (unlike regular enums, where you can do (MyEnum)42). Second, for encapsulation of logic. Instead of writing extension methods or utility classes, you place the logic directly inside the enumeration. Third, for readability. The code becomes self-documenting: each enumeration value carries not only a name but also context.\n
2. Installation
\nThe library is installed via NuGet. Run one of the following commands in the Package Manager Console or in the .NET CLI terminal:
\n# Via .NET CLI\ndotnet add package Ardalis.SmartEnum\n\n# Via Package Manager Console\nInstall-Package Ardalis.SmartEnum\n After installation, add using Ardalis.SmartEnum; at the top of your file.
3. Quick Start — Minimal Working Example
\nLet's create a simple enumeration for days of the week. Note: we inherit from SmartEnum<TEnum, TValue>, where TEnum is our class and TValue is the value type (usually int or string).
using Ardalis.SmartEnum;\n\npublic sealed class Weekday : SmartEnum<Weekday, int>\n{\n // Static fields are the enumeration values\n public static readonly Weekday Monday = new Weekday(nameof(Monday), 1);\n public static readonly Weekday Tuesday = new Weekday(nameof(Tuesday), 2);\n public static readonly Weekday Wednesday = new Weekday(nameof(Wednesday), 3);\n public static readonly Weekday Thursday = new Weekday(nameof(Thursday), 4);\n public static readonly Weekday Friday = new Weekday(nameof(Friday), 5);\n public static readonly Weekday Saturday = new Weekday(nameof(Saturday), 6);\n public static readonly Weekday Sunday = new Weekday(nameof(Sunday), 7);\n\n // Private constructor is mandatory\n private Weekday(string name, int value) : base(name, value) { }\n}\n\n// Usage\npublic class Program\n{\n public static void Main()\n {\n var today = Weekday.Monday;\n Console.WriteLine(today.Name); // Monday\n Console.WriteLine(today.Value); // 1\n\n // Lookup by value\n var dayFromValue = Weekday.FromValue(3);\n Console.WriteLine(dayFromValue.Name); // Wednesday\n\n // Lookup by name\n var dayFromName = Weekday.FromName("Friday");\n Console.WriteLine(dayFromName.Value); // 5\n }\n}\n\n