Humanizes an IEnumerable into a human readable list
public static class CollectionHumanizeExtensionsInheritance System.Object → CollectionHumanizeExtensions
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);T
The type of elements in the collection.
collection System.Collections.Generic.IEnumerable<T>
The collection to be humanized. Must not be null.
System.String
A formatted string representation of the collection elements separated by culture-specific separators.
For English, this typically produces: "item1, item2 and item3".
System.ArgumentNullException
Thrown when collection is null.
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() => ""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);T
The type of elements in the collection.
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.
System.String
A string representation of the collection elements separated by the specified separator.
System.ArgumentNullException
Thrown when collection is null.
new[] { 1, 2, 3 }.Humanize(" | ") => "1 | 2 | 3"
new[] { "Alice", "Bob" }.Humanize("; ") => "Alice; Bob"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);T
The type of elements in the collection.
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.
System.String
A formatted string representation of the collection elements, where each element is formatted
using displayFormatter and separated by culture-specific separators.
System.ArgumentNullException
Thrown when collection or displayFormatter is null.
var numbers = new[] { 1, 2, 3 };
numbers.Humanize(n => n * 2) => "2, 4 and 6"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);T
The type of elements in the collection.
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.
System.String
A string representation of the collection elements, where each element is formatted
using displayFormatter and separated by the specified separator.
System.ArgumentNullException
Thrown when collection or displayFormatter is null.
var numbers = new[] { 1, 2, 3 };
numbers.Humanize(n => n * 2, " - ") => "2 - 4 - 6"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);T
The type of elements in the collection.
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.
System.String
A formatted string representation of the collection elements, where each element is formatted
using displayFormatter and separated by culture-specific separators.
System.ArgumentNullException
Thrown when collection or displayFormatter is null.
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)"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);T
The type of elements in the collection.
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.
System.String
A string representation of the collection elements, where each element is formatted
using displayFormatter and separated by the specified separator.
System.ArgumentNullException
Thrown when collection or displayFormatter is null.
var people = new[] { new Person { Name = "Alice" }, new Person { Name = "Bob" } };
people.Humanize(p => p.Name, " | ") => "Alice | Bob"Transforms a collection into a human-readable string representation using the default separator for the specified culture.
public static string Humanize<T>(this System.Collections.Generic.IEnumerable<T> collection, System.Globalization.CultureInfo culture);T
The type of elements in the collection.
collection System.Collections.Generic.IEnumerable<T>
The collection to be humanized. Must not be null.
culture System.Globalization.CultureInfo
The culture whose collection formatter should be used. Must not be null.
System.String
A formatted string representation of the collection elements separated by culture-specific separators.
System.ArgumentNullException
Thrown when collection or culture is null.
new[] { 1, 2, 3 }.Humanize(new CultureInfo("en-GB")) => "1, 2 and 3"