Skip to main content
Version: 4.0 (next)

Format collections and tuple names

Collection humanization turns a sequence into a localized natural-language list. Pass a display selector when items are domain objects. Tuple helpers produce words such as double, triple, or quadruple for multiplicity labels; they do not transform a collection itself.

Example

The executable formats strings, selects British English without changing ambient culture, projects a record to its display name, demonstrates the default formatter's null/blank filtering, and produces an invariant English tuple word:

Program.cs
using System.Globalization;
using Humanizer;

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

var list = new[] { "tea", "coffee", "water" }.Humanize();
var britishList = new[] { "tea", "coffee", "water" }
.Humanize(CultureInfo.GetCultureInfo("en-GB"));
var people = new[] { new Person("Ada"), new Person("Grace") }
.Humanize(person => person.Name);
var cleaned = new string?[] { " tea ", null, " ", "coffee" }.Humanize();
var tuple = 4.Tupleize();

Console.WriteLine($"US: {list}");
Console.WriteLine($"UK: {britishList}");
Console.WriteLine($"People: {people}");
Console.WriteLine($"Cleaned: {cleaned}");
Console.WriteLine($"Tuple: {tuple}");

record Person(string Name);
Output
US: tea, coffee, and water
UK: tea, coffee and water
People: Ada and Grace
Cleaned: tea and coffee
Tuple: quadruple

Format a collection

The no-argument overload calls ToString for each item. Formatter overloads accept a function returning string or object. The default formatter trims each display result and drops null, empty, or whitespace-only results before deciding conjunction placement, punctuation, and the empty/singleton cases.

The separator argument represents the desired conjunction token, such as or; it is not simply a raw delimiter. For full application-wide customization, register an ICollectionFormatter at startup.

Select a culture explicitly

Pass a non-null CultureInfo to select the collection formatter without changing CurrentCulture. For example, an en-GB formatter joins three items without the Oxford comma. The explicit-culture overload accepts the collection itself; when items need projection, select the display strings first and then call Humanize(culture).

Name a tuple

Tupleize returns fixed English names for common sizes and an n-tuple fallback. ToTuple(culture) uses the selected locale’s number-to-words converter where supported.

Set culture and validate required items

Collection output follows CurrentCulture; set it at the request or job boundary. The default formatter silently removes null or blank display results, so validate required source data before formatting. A custom formatter can define a different contract. The collectionSeparator parameter on TimeSpan.Humanize has different literal-separator semantics.