Skip to main content
Version: 2.13.14

Humanize and parse enums and flags

Orientation

Enum humanization resolves one display label for each member: supported description/display metadata when present, otherwise the humanized member name. Dehumanization matches that resolved label case-insensitively. It does not index every possible raw, humanized, and metadata form. Flags enums are decomposed and joined through the active collection formatter.

Example

The executable verifies a two-flag value, case-insensitive generic dehumanization, and an unknown flag bit:

Program.cs
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Humanizer;

CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");

var access = Access.Read | Access.Write;

AssertEqual("Can view and Can edit", access.Humanize());
AssertEqual(Access.Read, "CAN VIEW".DehumanizeTo<Access>());
AssertEqual("", ((Access)8).Humanize());

Console.WriteLine("Can view and Can edit; CAN VIEW -> Read");

static void AssertEqual<T>(T expected, T actual)
{
if (!EqualityComparer<T>.Default.Equals(expected, actual))
throw new InvalidOperationException($"Expected '{expected}', got '{actual}'.");
}

[Flags]
enum Access
{
[Display(Description = "None")]
None = 0,
[Display(Description = "Can view")]
Read = 1,
[Display(Description = "Can edit")]
Write = 2
}

Choose metadata and casing

Humanizer prefers supported description/display metadata before deriving words from the member name. That chosen value is also the member's dehumanization label. Pass LetterCasing when the derived label needs a specific case. Assign Humanizer.Configuration.Configurator.EnumDescriptionPropertyLocator to select a custom description-like property globally before enum metadata is cached.

Format flags

Known nonzero flags are humanized and joined using the current UI culture. A defined zero member uses its label. A value containing only unknown bits humanizes to an empty string.

Pitfall

Labels must be unambiguous even though matching is case-insensitive. The nullable generic no-match overload and 3.x trimming annotations are not present in this release.

Version notes

These enum APIs use the Humanizer 2 signatures. Humanizer 3 later changes generic constraints and metadata configuration.