Skip to main content
Version: 3.0.10 (latest)

Humanizer.CollectionHumanizeExtensions

CollectionHumanizeExtensions Class

Humanizes an IEnumerable into a human readable list

public static class CollectionHumanizeExtensions

Inheritance System.Object → CollectionHumanizeExtensions

Methods

CollectionHumanizeExtensions.Humanize<T>(this IEnumerable<T>) Method

Transforms a collection into a human-readable string representation by calling System.Object.ToString on each element and combining them with the default separator for the current culture (typically ", " with "and" before the last item).

public static string Humanize<T>(this System.Collections.Generic.IEnumerable<T> collection);

Type parameters

T

The type of elements in the collection.

Parameters

collection System.Collections.Generic.IEnumerable<T>

The collection to be humanized. Must not be null.

Returns

System.String
A formatted string representation of the collection elements separated by culture-specific separators. For English, this typically produces: "item1, item2 and item3".

Exceptions

System.ArgumentNullException
Thrown when collection is null.

Example

new[] { 1, 2, 3 }.Humanize() => "1, 2 and 3"
new[] { "Alice", "Bob", "Charlie" }.Humanize() => "Alice, Bob and Charlie"
new[] { "single" }.Humanize() => "single"
new string[] { }.Humanize() => ""

CollectionHumanizeExtensions.Humanize<T>(this IEnumerable<T>, string) Method

Transforms a collection into a string representation by calling System.Object.ToString on each element and combining them with the specified separator.

public static string Humanize<T>(this System.Collections.Generic.IEnumerable<T> collection, string separator);

Type parameters

T

The type of elements in the collection.

Parameters

collection System.Collections.Generic.IEnumerable<T>

The collection to be humanized. Must not be null.

separator System.String

The string to use as a separator between elements.

Returns

System.String
A string representation of the collection elements separated by the specified separator.

Exceptions

System.ArgumentNullException
Thrown when collection is null.

Example

new[] { 1, 2, 3 }.Humanize(" | ") => "1 | 2 | 3"
new[] { "Alice", "Bob" }.Humanize("; ") => "Alice; Bob"

CollectionHumanizeExtensions.Humanize<T>(this IEnumerable<T>, Func<T,object>) Method

Transforms a collection into a human-readable string representation using a custom formatter function that returns an object for each element (which will be converted to string), combined with the default separator for the current culture.

public static string Humanize<T>(this System.Collections.Generic.IEnumerable<T> collection, System.Func<T,object> displayFormatter);

Type parameters

T

The type of elements in the collection.

Parameters

collection System.Collections.Generic.IEnumerable<T>

The collection to be humanized. Must not be null.

displayFormatter System.Func<T,System.Object>

A function that converts each element of type T to an object that will be converted to its string representation. Must not be null.

Returns

System.String
A formatted string representation of the collection elements, where each element is formatted using displayFormatter and separated by culture-specific separators.

Exceptions

System.ArgumentNullException
Thrown when collection or displayFormatter is null.

Example

var numbers = new[] { 1, 2, 3 };
numbers.Humanize(n => n * 2) => "2, 4 and 6"

CollectionHumanizeExtensions.Humanize<T>(this IEnumerable<T>, Func<T,object>, string) Method

Transforms a collection into a string representation using a custom formatter function that returns an object for each element (which will be converted to string), combined with the specified separator.

public static string Humanize<T>(this System.Collections.Generic.IEnumerable<T> collection, System.Func<T,object> displayFormatter, string separator);

Type parameters

T

The type of elements in the collection.

Parameters

collection System.Collections.Generic.IEnumerable<T>

The collection to be humanized. Must not be null.

displayFormatter System.Func<T,System.Object>

A function that converts each element of type T to an object that will be converted to its string representation. Must not be null.

separator System.String

The string to use as a separator between formatted elements.

Returns

System.String
A string representation of the collection elements, where each element is formatted using displayFormatter and separated by the specified separator.

Exceptions

System.ArgumentNullException
Thrown when collection or displayFormatter is null.

Example

var numbers = new[] { 1, 2, 3 };
numbers.Humanize(n => n * 2, " - ") => "2 - 4 - 6"

CollectionHumanizeExtensions.Humanize<T>(this IEnumerable<T>, Func<T,string>) Method

Transforms a collection into a human-readable string representation using a custom formatter function for each element, combined with the default separator for the current culture.

public static string Humanize<T>(this System.Collections.Generic.IEnumerable<T> collection, System.Func<T,string> displayFormatter);

Type parameters

T

The type of elements in the collection.

Parameters

collection System.Collections.Generic.IEnumerable<T>

The collection to be humanized. Must not be null.

displayFormatter System.Func<T,System.String>

A function that converts each element of type T to a string representation. Must not be null.

Returns

System.String
A formatted string representation of the collection elements, where each element is formatted using displayFormatter and separated by culture-specific separators.

Exceptions

System.ArgumentNullException
Thrown when collection or displayFormatter is null.

Example

var people = new[] { new Person { Name = "Alice", Age = 30 }, new Person { Name = "Bob", Age = 25 } };
people.Humanize(p => p.Name) => "Alice and Bob"
people.Humanize(p => $"{p.Name} ({p.Age})") => "Alice (30) and Bob (25)"

CollectionHumanizeExtensions.Humanize<T>(this IEnumerable<T>, Func<T,string>, string) Method

Transforms a collection into a string representation using a custom formatter function for each element, combined with the specified separator.

public static string Humanize<T>(this System.Collections.Generic.IEnumerable<T> collection, System.Func<T,string> displayFormatter, string separator);

Type parameters

T

The type of elements in the collection.

Parameters

collection System.Collections.Generic.IEnumerable<T>

The collection to be humanized. Must not be null.

displayFormatter System.Func<T,System.String>

A function that converts each element of type T to a string representation. Must not be null.

separator System.String

The string to use as a separator between formatted elements.

Returns

System.String
A string representation of the collection elements, where each element is formatted using displayFormatter and separated by the specified separator.

Exceptions

System.ArgumentNullException
Thrown when collection or displayFormatter is null.

Example

var people = new[] { new Person { Name = "Alice" }, new Person { Name = "Bob" } };
people.Humanize(p => p.Name, " | ") => "Alice | Bob"