Skip to main content
Version: 4.0 (next)

Format collections and tuple names

Orientation

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, verifies 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();

AssertEqual("tea, coffee, and water", list);
AssertEqual("tea, coffee and water", britishList);
AssertEqual("Ada and Grace", people);
AssertEqual("tea and coffee", cleaned);
AssertEqual("quadruple", tuple);

Console.WriteLine($"{list}; {britishList}; {tuple}");

static void AssertEqual(string expected, string actual)
{
if (actual != expected)
throw new InvalidOperationException($"Expected '{expected}', got '{actual}'.");
}

record Person(string Name);

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

In Humanizer 4, 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.

Pitfall

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.

Version notes

Collection humanization and tuple helpers span the documented corpus, while punctuation and localized tuple coverage vary by release. The executable compiles and runs against the selected package. The explicit-culture collection overload is available in the Humanizer 4 NuGet package.