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 AOT-safe 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:
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.
For trimmed or Native AOT code, prefer generic enum APIs. The runtime Type dehumanization overload uses reflection and carries linker/AOT warnings.
Version notes
The verified survey compiles and runs against the selected package. Enum generic constraints and metadata configuration change in Humanizer 3. Locale punctuation is versioned data even when the source call compiles unchanged.