Inheritance
EnumerableExtensions
public static class EnumerableExtensions
Methods
Concatenates the first sequence with the specified items.
This method is an overload for Concat that uses the params argument to provide
a better syntax when used with a known set of items.
Declaration
public static IEnumerable<T> Concat<T>(this IEnumerable<T> first, params T[] secondItems)
Parameters
Type |
Name |
Description |
IEnumerable<T> |
first |
|
T[] |
secondItems |
|
Returns
Type Parameters
Performs an action for each item in the list.
For simple actions, this provides an easier syntax than the normal foreach loop.
Declaration
public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource, int> action)
Parameters
Type |
Name |
Description |
IEnumerable<TSource> |
source |
|
Action<TSource, Int32> |
action |
This action method takes into account the index of the item.
|
Type Parameters
Performs an action for each item in the list.
For simple actions, this provides an easier syntax than the normal foreach loop.
Declaration
public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource> action)
Parameters
Type Parameters
Uses String.Format to format each item in the collection using the specified format.
Declaration
public static IEnumerable<string> FormatEach<T>(this IEnumerable<T> source, string format)
Parameters
Type |
Name |
Description |
IEnumerable<T> |
source |
|
String |
format |
A composite format string; same as String.Format.
{0} refers to the source object, {1} refers to the index.
|
Returns
Type Parameters
Joins all strings together into a single string, separating each item with the separator.
For example:
alphabet.JoinStrings(", ") == "a, b, ... y, z"
Declaration
public static string JoinStrings(this IEnumerable<string> source, string separator)
Parameters
Type |
Name |
Description |
IEnumerable<String> |
source |
The source of strings to join together
|
String |
separator |
The text to insert between items
|
Returns
Joins all strings together into a single string, separating each item with the separator,
and separating the last item with an alternate separator.
For example:
alphabet.JoinStrings(", ", " and ") == "a, b, ... x, y and z"
Declaration
public static string JoinStrings(this IEnumerable<string> source, string separator, string lastSeparator)
Parameters
Returns
Joins all strings together into a single string, separating each item with the separator,
and separating the last item with an alternate separator.
Allows you to specify a cutoff, so that further items will not be displayed.
For example:
alphabet.JoinStrings(", ", " and ", 4, " and {0} others...") == "a, b, c, d and 22 others..."
Declaration
public static string JoinStrings(this IEnumerable<string> source, string separator, string lastSeparator, string cutoffText, int cutoff)
Parameters
Type |
Name |
Description |
IEnumerable<String> |
source |
|
String |
separator |
This string will be inserted between items.
|
String |
lastSeparator |
This string will be inserted before the last item, instead of the separator.
|
String |
cutoffText |
The text that will be used if the cutoff is reached.
For example, " and {0} others..."
Optional placeholders: {0} = cutoff count, {1} = source count
|
Int32 |
cutoff |
Limits the number of items.
If the limit is reached, then cutoffText is appended.
|
Returns
Chooses one of the items at random.
Throws an exception if there are no items.
Declaration
public static T Random<T>(this IEnumerable<T> source)
Parameters
Returns
Type Parameters
Chooses one of the items at random.
Returns default if there are no items.
Declaration
public static T RandomOrDefault<T>(this IEnumerable<T> source)
Parameters
Returns
Type Parameters
Splits the enumeration into segments,
each segment containing the portion between predicate matches.
Declaration
public static IEnumerable<TSource[]> Split<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> splitPredicate)
Parameters
Type |
Name |
Description |
IEnumerable<TSource> |
source |
|
Func<TSource, Boolean> |
splitPredicate |
The condition that will create a new split.
The matching item will be the first element in the new split.
|
Returns
Type Parameters
Splits the enumeration into segments,
each segment containing the portion between predicate matches.
Declaration
public static IEnumerable<TSource[]> Split<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> splitPredicate)
Parameters
Type |
Name |
Description |
IEnumerable<TSource> |
source |
|
Func<TSource, Int32, Boolean> |
splitPredicate |
The condition that will create a new split.
The matching item will be the first element in the new split.
This predicate incorporates the element's index.
|
Returns
Type Parameters
Splits the enumeration into segments,
each segment containing [count] items, and the last segment containing the remainder.
Declaration
public static IEnumerable<TSource[]> Split<TSource>(this IEnumerable<TSource> source, int count)
Parameters
Type |
Name |
Description |
IEnumerable<TSource> |
source |
|
Int32 |
count |
The number of items to include in each segment.
The last segment might contain fewer items.
|
Returns
Type Parameters
Splits the enumeration into segments,
each segment containing the portion between predicate matches.
Declaration
public static IEnumerable<TSource[]> SplitAfter<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> splitPredicate)
Parameters
Type |
Name |
Description |
IEnumerable<TSource> |
source |
|
Func<TSource, Boolean> |
splitPredicate |
The condition that will create a new split.
The matching item will be the last element in the previous split.
|
Returns
Type Parameters
Splits the enumeration into segments,
each segment containing the portion between predicate matches.
Declaration
public static IEnumerable<TSource[]> SplitAfter<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> splitPredicate)
Parameters
Type |
Name |
Description |
IEnumerable<TSource> |
source |
|
Func<TSource, Int32, Boolean> |
splitPredicate |
The condition that will create a new split.
The matching item will be the last element in the previous split.
This predicate incorporates the element's index.
|
Returns
Type Parameters
Produces the set union of the first sequence and the specified items by using the default equality comparer.
This method is an overload for Union that uses the params argument to provide
a better syntax when used with a known set of items.
Declaration
public static IEnumerable<T> Union<T>(this IEnumerable<T> first, params T[] secondItems)
Parameters
Type |
Name |
Description |
IEnumerable<T> |
first |
|
T[] |
secondItems |
|
Returns
Type Parameters