Skip to main content
Version: 2.13.14

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, 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 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("Ada and Grace", people);
AssertEqual("tea and coffee", cleaned);
AssertEqual("quadruple", tuple);

Console.WriteLine($"{list}; {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.

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 CurrentUICulture; 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 this selected package.