finds.dev← search

// the find

ardalis/SmartEnum

★ 2,428 · C# · MIT · updated Apr 2026

A base class for quickly and easily creating strongly typed enum replacements in C#.

SmartEnum gives you type-safe enum replacements in C# by making each enum a class with static singleton instances. The pattern lets you attach behavior directly to enum values — overridable methods, state machine transitions, bonus calculations — instead of scattering switch statements across your codebase. It's well-suited for DDD projects where enums naturally carry domain logic.

The inheritance-based behavior pattern is the real win here: attaching abstract methods to each value (like BonusSize or CanTransitionTo) eliminates a whole class of switch-statement drift. The ecosystem coverage is thorough — EFCore, Dapper, System.Text.Json, Json.NET, MessagePack, ProtoBufNet, AutoFixture all handled in separate packages so you only take the weight you need. SmartFlagEnum with power-of-two validation catches a common bug with flag enums at startup rather than at runtime when your bit math silently misbehaves. The fluent When/Then switch alternative reads cleanly for cases with many branches.

C# discriminated unions (via OneOf or just sealed class hierarchies) solve the same 'behavior on a value' problem with better compiler exhaustiveness checking — if you add a new case to a SmartEnum, nothing warns you about unhandled branches. The EFCore parameterless constructor requirement (issue #103, mentioned in the README) is a real footgun: you either expose a public constructor that breaks the 'only static instances' contract or implement a workaround that's easy to forget. The fluent switch interface carries measurable overhead the benchmarks acknowledge but don't quantify clearly for typical enum sizes. SmartFlagEnum's combination values returning single vs. multiple results depending on whether an explicit combination exists is surprising enough to cause real bugs.

View on GitHub →

// want more like this?

We dig through GitHub every week and send a few repos picked for what you actually care about — each with an honest take like this one.

Get finds in your inbox → Search again →