Overview

Standard C# enums are just named integers. Any number can be cast to an enum, and the rules attached to each value end up duplicated in switch statements throughout the codebase. When a new value is added, every one of those switches has to be found and updated.

Ardalis.SmartEnum turns enumerations into real types that keep their data and behavior together:

public sealed class SubscriptionPlan : SmartEnum<SubscriptionPlan>
{
    public static readonly SubscriptionPlan Free = new(nameof(Free), 1, monthlyPrice: 0m);
    public static readonly SubscriptionPlan Pro = new(nameof(Pro), 2, monthlyPrice: 29m);

    public decimal MonthlyPrice { get; }

    private SubscriptionPlan(string name, int value, decimal monthlyPrice)
        : base(name, value) => MonthlyPrice = monthlyPrice;
}

What your organization gains

  • Safer domain models. Invalid values cannot be created, so a whole class of bugs disappears.
  • Lower cost of change. Adding a new option means updating one class, not hunting down every switch.
  • Code that speaks the business language. Rules live next to the concept they describe, making the domain easier to understand and discuss.
  • Less code to own. Parsing, comparison, and serialization are already built and tested.

How It Compares

Smart enums: no pattern vs. a custom implementation vs. Ardalis.SmartEnum
FeatureNo PatternCustom ImplementationSmartEnum
Benefits of the Smart Enum pattern
Invalid values cannot be created No Yes Yes
Behavior lives with each value instead of in scattered switch statements No Yes Yes
New options added in one place No Yes Yes
Rich data (display names, prices, rules) attached to each value No Yes Yes
Domain concepts expressed in the language of the business No Yes Yes
Benefits of a maintained package used by tens of thousands of developers
Battle-tested in production by tens of thousands of developers No No Yes
Bugs found and fixed by a large user community No No Yes
Pattern code your team doesn’t have to write, test, or maintain No No Yes
Kept current with new .NET releases No No Yes
Ready-made JSON serialization and EF Core persistence support No No Yes
Documentation and samples that speed up onboarding No No Yes
Already familiar to many developers before they join your team No No Yes
Professionally maintained and supported by NimbleTools No No Yes

Get the package on NuGet with dotnet add package Ardalis.SmartEnum.

Key Features

  • Strongly typed enumerations with names, values, and behavior
  • Safe parsing with FromName and FromValue
  • Serialization support for System.Text.Json, Entity Framework Core, and more
  • Supports current .NET releases