Method MaxItems
| Edit this page View SourceMaxItems<T>(IEnumerable<T>)
Returns all of the items that share the maximum value of a sequence.
Declaration
public static IEnumerable<T> MaxItems<T>(this IEnumerable<T> source)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The source sequence. |
Returns
Type | Description |
---|---|
IEnumerable<T> | An IEnumerable<T> containing all of the items that share the maximum value. |
Type Parameters
Name | Description |
---|---|
T | Type of elements in the sequence. |
Remarks
This operator is a shortcut for DensePartialSort<T>(IEnumerable<T>, int, OrderByDirection)
with a direction
of Descending and a count
of 1
.
This method is implemented by using deferred execution. However, source
will be consumed
in it's entirety immediately when first element of the returned sequence is consumed.
Examples
The following code example demonstrates how to get the maximum items from a sequence using MaxItems
.
var sequence = new[]
{
"A", "B", "C", "D", "F",
"a", "b", "c", "d", "f",
}.AsEnumerable();
// Find the maximum items of the sequence
var result = sequence
.MaxItems();
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [F]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
MaxItems<T>(IEnumerable<T>, IComparer<T>?)
Returns all of the items that share the maximum value of a sequence.
Declaration
public static IEnumerable<T> MaxItems<T>(this IEnumerable<T> source, IComparer<T>? comparer)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The source sequence. |
IComparer<T> | comparer | A IComparer<T> to compare elements. |
Returns
Type | Description |
---|---|
IEnumerable<T> | An IEnumerable<T> containing all of the items that share the maximum value. |
Type Parameters
Name | Description |
---|---|
T | Type of elements in the sequence. |
Remarks
This operator is a shortcut for DensePartialSort<T>(IEnumerable<T>, int, IComparer<T>?, OrderByDirection) with a count
of 1
.
This method is implemented by using deferred execution. However, source
will be consumed
in it's entirety immediately when first element of the returned sequence is consumed.
Examples
The following code example demonstrates how to get the maximum items from a sequence using MaxItems
.
var sequence = new[]
{
"A", "B", "C", "D", "F",
"a", "b", "c", "d", "f",
}.AsEnumerable();
// Find the maximum items of the sequence
var result = sequence
.MaxItems(StringComparer.OrdinalIgnoreCase);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [F]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|