Skip to main content
Version: 2.11.10

Enum and collection scenarios

Orientation

Enums and collections both become readable presentation text, but their contracts are different. Enum conversion maps between a typed member and a label, while collection humanization delegates conjunction and punctuation to a locale-aware formatter.

  • Enums and flags covers metadata, case-insensitive parsing, no-match behavior, flags, and typed overload choice.
  • Collections and tuple names covers list punctuation, display selectors, custom formatters, and multiplicity words.

Example

This verified survey shows a description override, automatic enum casing, case-insensitive enum dehumanization, and an English collection:

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

CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
string[] drinks = ["tea", "coffee", "water"];

AssertEqual("Waiting for payment", OrderState.AwaitingPayment.Humanize());
AssertEqual("Ready to ship", OrderState.ReadyToShip.Humanize());
AssertEqual(OrderState.AwaitingPayment, "WAITING FOR PAYMENT".DehumanizeTo<OrderState>());
AssertEqual("tea, coffee, and water", drinks.Humanize());
AssertEqual(
"Ada and Grace",
new[] { new Person("Ada"), new Person("Grace") }
.Humanize(person => person.DisplayName));

Console.WriteLine("Waiting for payment; tea, coffee, and water");

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

enum OrderState
{
[Description("Waiting for payment")]
AwaitingPayment,
ReadyToShip
}

record Person(string DisplayName);

The source also projects Person records to display names. Follow the focused pages for flags, unknown values, localized punctuation, and custom formatters.

Pitfall

Enum labels must be unambiguous even though matching is case-insensitive. The default no-match behavior throws. Collection output is culture-specific and can differ by selected release; scope CurrentUICulture before formatting in tests or background work.

This 2.x API predates trimming annotations on the runtime Type overload. Generic enum APIs preserve compile-time type information, but they do not prove that a 2.x application is linker-safe. Validate trimming or Native AOT at the application boundary.

Version notes

The verified survey compiles and runs against this selected package. Enum generic constraints and metadata configuration change in Humanizer 3. Locale punctuation is versioned data even when the source call compiles unchanged.