Enumerable.Max 方法

定义

返回值序列中的最大值。

重载

Max(IEnumerable<Nullable<Int32>>)

返回可以为 null 的 Int32 值序列中的最大值。

Max(IEnumerable<Single>)

返回 Single 值序列中的最大值。

Max(IEnumerable<Nullable<Single>>)

返回可以为 null 的 Single 值序列中的最大值。

Max(IEnumerable<Nullable<Int64>>)

返回可以为 null 的 Int64 值序列中的最大值。

Max(IEnumerable<Nullable<Double>>)

返回可以为 null 的 Double 值序列中的最大值。

Max(IEnumerable<Double>)

返回 Double 值序列中的最大值。

Max(IEnumerable<Int64>)

返回 Int64 值序列中的最大值。

Max(IEnumerable<Int32>)

返回 Int32 值序列中的最大值。

Max(IEnumerable<Decimal>)

返回 Decimal 值序列中的最大值。

Max(IEnumerable<Nullable<Decimal>>)

返回可以为 null 的 Decimal 值序列中的最大值。

Max<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>)

对序列中的每个元素调用转换函数,并返回最大结果值。

Max<TSource>(IEnumerable<TSource>, Func<TSource,Single>)

对序列中的每个元素调用转换函数,并返回最大的 Single 值。

Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)

对序列中的每个元素调用转换函数,并返回可以为 null 的最大的 Single 值。

Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)

对序列中的每个元素调用转换函数,并返回可以为 null 的最大的 Int64 值。

Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)

对序列中的每个元素调用转换函数,并返回可以为 null 的最大的 Int32 值。

Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)

对序列中的每个元素调用转换函数,并返回可以为 null 的最大的 Decimal 值。

Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)

对序列中的每个元素调用转换函数,并返回可以为 null 的最大的 Double 值。

Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)

对序列中的每个元素调用转换函数,并返回最大的 Int32 值。

Max<TSource>(IEnumerable<TSource>, Func<TSource,Double>)

对序列中的每个元素调用转换函数,并返回最大的 Double 值。

Max<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)

对序列中的每个元素调用转换函数,并返回最大的 Decimal 值。

Max<TSource>(IEnumerable<TSource>, IComparer<TSource>)

返回泛型序列中的最大值。

Max<TSource>(IEnumerable<TSource>)

返回泛型序列中的最大值。

Max<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)

对序列中的每个元素调用转换函数,并返回最大的 Int64 值。

Max(IEnumerable<Nullable<Int32>>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

返回可以为 null 的 Int32 值序列中的最大值。

C#
public static int? Max (this System.Collections.Generic.IEnumerable<int?> source);

参数

source
IEnumerable<Nullable<Int32>>

要确定其最大值的可以为 null 的 Int32 值序列。

返回

C# 或 Nullable(Of Int32) Visual Basic 中类型的Nullable<Int32>值,对应于序列中的最大值。

例外

sourcenull

注解

方法 Max(IEnumerable<Nullable<Int32>>) 使用 的 Int32IComparable<T> 实现来比较值。

如果源序列为空或仅包含 的值,则 null此函数返回 null

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的调用 Max

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max(IEnumerable<Single>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

返回 Single 值序列中的最大值。

C#
public static float Max (this System.Collections.Generic.IEnumerable<float> source);

参数

source
IEnumerable<Single>

要确定其最大值的 Single 值序列。

返回

序列中的最大值。

例外

sourcenull

source 中不包含任何元素。

注解

方法 Max(IEnumerable<Single>) 使用 的 SingleIComparable<T> 实现来比较值。

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的调用 Max

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max(IEnumerable<Nullable<Single>>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

返回可以为 null 的 Single 值序列中的最大值。

C#
public static float? Max (this System.Collections.Generic.IEnumerable<float?> source);

参数

source
IEnumerable<Nullable<Single>>

要确定其最大值的可以为 null 的 Single 值序列。

返回

C# 或 Nullable(Of Single) Visual Basic 中类型的Nullable<Single>值,对应于序列中的最大值。

例外

sourcenull

注解

方法 Max(IEnumerable<Nullable<Single>>) 使用 的 SingleIComparable<T> 实现来比较值。

如果源序列为空或仅包含 的值,则 null此函数返回 null

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的调用 Max

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max(IEnumerable<Nullable<Int64>>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

返回可以为 null 的 Int64 值序列中的最大值。

C#
public static long? Max (this System.Collections.Generic.IEnumerable<long?> source);

参数

source
IEnumerable<Nullable<Int64>>

要确定其最大值的可以为 null 的 Int64 值序列。

返回

C# 或 Nullable(Of Int64) Visual Basic 中类型的Nullable<Int64>值,对应于序列中的最大值。

例外

sourcenull

注解

方法 Max(IEnumerable<Nullable<Int64>>) 使用 的 Int64IComparable<T> 实现来比较值。

如果源序列为空或仅包含 的值,则 null此函数返回 null

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的调用 Max

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max(IEnumerable<Nullable<Double>>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

返回可以为 null 的 Double 值序列中的最大值。

C#
public static double? Max (this System.Collections.Generic.IEnumerable<double?> source);

参数

source
IEnumerable<Nullable<Double>>

要确定其最大值的可以为 null 的 Double 值序列。

返回

C# 或 Nullable(Of Double) Visual Basic 中类型的Nullable<Double>值,对应于序列中的最大值。

例外

sourcenull

示例

下面的代码示例演示如何使用 Max(IEnumerable<Nullable<Double>>) 来确定序列中的最大值。

C#
double?[] doubles = { null, 1.5E+104, 9E+103, -2E+103 };

double? max = doubles.Max();

Console.WriteLine("The largest number is {0}.", max);

/*
 This code produces the following output:

 The largest number is 1.5E+104.
*/

注解

方法 Max(IEnumerable<Nullable<Double>>) 使用 的 DoubleIComparable<T> 实现来比较值。

如果源序列为空或仅包含 的值,则 null此函数返回 null

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的调用 Max

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max(IEnumerable<Double>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

返回 Double 值序列中的最大值。

C#
public static double Max (this System.Collections.Generic.IEnumerable<double> source);

参数

source
IEnumerable<Double>

要确定其最大值的 Double 值序列。

返回

序列中的最大值。

例外

sourcenull

source 中不包含任何元素。

注解

方法 Max(IEnumerable<Double>) 使用 的 DoubleIComparable<T> 实现来比较值。

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的调用 Max

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max(IEnumerable<Int64>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

返回 Int64 值序列中的最大值。

C#
public static long Max (this System.Collections.Generic.IEnumerable<long> source);

参数

source
IEnumerable<Int64>

要确定其最大值的 Int64 值序列。

返回

序列中的最大值。

例外

sourcenull

source 中不包含任何元素。

示例

下面的代码示例演示如何使用 Max(IEnumerable<Int64>) 来确定序列中的最大值。

C#
List<long> longs = new List<long> { 4294967296L, 466855135L, 81125L };

long max = longs.Max();

Console.WriteLine("The largest number is {0}.", max);

/*
 This code produces the following output:

 The largest number is 4294967296.
*/

注解

方法 Max(IEnumerable<Int64>) 使用 的 Int64IComparable<T> 实现来比较值。

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的 Max调用。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max(IEnumerable<Int32>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

返回 Int32 值序列中的最大值。

C#
public static int Max (this System.Collections.Generic.IEnumerable<int> source);

参数

source
IEnumerable<Int32>

要确定其最大值的 Int32 值序列。

返回

序列中的最大值。

例外

sourcenull

source 中不包含任何元素。

注解

方法 Max(IEnumerable<Int32>) 使用 的 Int32IComparable<T> 实现来比较值。

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的 Max调用。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max(IEnumerable<Decimal>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

返回 Decimal 值序列中的最大值。

C#
public static decimal Max (this System.Collections.Generic.IEnumerable<decimal> source);

参数

source
IEnumerable<Decimal>

要确定其最大值的 Decimal 值序列。

返回

序列中的最大值。

例外

sourcenull

source 中不包含任何元素。

注解

方法 Max(IEnumerable<Decimal>) 使用 的 DecimalIComparable<T> 实现来比较值。

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的 Max调用。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max(IEnumerable<Nullable<Decimal>>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

返回可以为 null 的 Decimal 值序列中的最大值。

C#
public static decimal? Max (this System.Collections.Generic.IEnumerable<decimal?> source);

参数

source
IEnumerable<Nullable<Decimal>>

要确定其最大值的可以为 null 的 Decimal 值序列。

返回

C# 或 Nullable(Of Decimal) Visual Basic 中的类型Nullable<Decimal>值,对应于序列中的最大值。

例外

sourcenull

注解

方法 Max(IEnumerable<Nullable<Decimal>>) 使用 的 DecimalIComparable<T> 实现来比较值。

如果源序列为空或仅包含为 null的值,则此函数返回 null

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的 Max调用。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

对序列中的每个元素调用转换函数,并返回最大结果值。

C#
public static TResult Max<TSource,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TResult> selector);
C#
public static TResult? Max<TSource,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TResult> selector);

类型参数

TSource

source 的元素类型。

TResult

selector 返回的值的类型。

参数

source
IEnumerable<TSource>

要确定其最大值的值序列。

selector
Func<TSource,TResult>

应用于每个元素的转换函数。

返回

TResult

序列中的最大值。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。

备注

此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void MaxEx4()
{
    Pet[] pets = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };

    int max = pets.Max(pet => pet.Age + pet.Name.Length);

    Console.WriteLine(
        "The maximum pet age plus name length is {0}.",
        max);
}

/*
 This code produces the following output:

 The maximum pet age plus name length is 14.
*/

注解

如果 type TResult 实现 IComparable<T>,则此方法使用该实现来比较值。 否则,如果 类型 TResult 实现 IComparable,则使用该实现来比较值。

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的 Max调用。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max<TSource>(IEnumerable<TSource>, Func<TSource,Single>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

对序列中的每个元素调用转换函数,并返回最大的 Single 值。

C#
public static float Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,float> selector);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最大值的值序列。

selector
Func<TSource,Single>

应用于每个元素的转换函数。

返回

序列中的最大值。

例外

sourceselectornull

source 中不包含任何元素。

示例

下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。

备注

此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void MaxEx4()
{
    Pet[] pets = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };

    int max = pets.Max(pet => pet.Age + pet.Name.Length);

    Console.WriteLine(
        "The maximum pet age plus name length is {0}.",
        max);
}

/*
 This code produces the following output:

 The maximum pet age plus name length is 14.
*/

注解

方法 Max<TSource>(IEnumerable<TSource>, Func<TSource,Single>) 使用 的 SingleIComparable<T> 实现来比较值。

如果提供将 的成员source投影为数值类型的函数 selector,则可以将此方法应用于任意值序列,具体来说就是 Single

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的 Max调用。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

对序列中的每个元素调用转换函数,并返回可以为 null 的最大的 Single 值。

C#
public static float? Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,float?> selector);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最大值的值序列。

selector
Func<TSource,Nullable<Single>>

应用于每个元素的转换函数。

返回

对应于序列中最大值的值。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。

备注

此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void MaxEx4()
{
    Pet[] pets = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };

    int max = pets.Max(pet => pet.Age + pet.Name.Length);

    Console.WriteLine(
        "The maximum pet age plus name length is {0}.",
        max);
}

/*
 This code produces the following output:

 The maximum pet age plus name length is 14.
*/

注解

方法 Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>) 使用 的 SingleIComparable<T> 实现来比较值。

如果提供函数 ,可以将此方法应用于任意值序列, selector该函数将 的成员 source 投影到数值类型中,特别是在 Nullable<Single> C# 或 Nullable(Of Single) Visual Basic 中。

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的 Max调用。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

对序列中的每个元素调用转换函数,并返回可以为 null 的最大的 Int64 值。

C#
public static long? Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,long?> selector);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最大值的值序列。

selector
Func<TSource,Nullable<Int64>>

应用于每个元素的转换函数。

返回

对应于序列中最大值的值。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。

备注

此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void MaxEx4()
{
    Pet[] pets = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };

    int max = pets.Max(pet => pet.Age + pet.Name.Length);

    Console.WriteLine(
        "The maximum pet age plus name length is {0}.",
        max);
}

/*
 This code produces the following output:

 The maximum pet age plus name length is 14.
*/

注解

方法 Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>) 使用 的 Int64IComparable<T> 实现来比较值。

如果提供函数 ,可以将此方法应用于任意值序列, selector该函数将 的成员 source 投影到数值类型中,特别是在 Nullable<Int64> C# 或 Nullable(Of Int64) Visual Basic 中。

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的 Max调用。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

对序列中的每个元素调用转换函数,并返回可以为 null 的最大的 Int32 值。

C#
public static int? Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int?> selector);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最大值的值序列。

selector
Func<TSource,Nullable<Int32>>

应用于每个元素的转换函数。

返回

C# 中或 Nullable(Of Int32) Visual Basic 中与序列中的最大值相对应的类型Nullable<Int32>的值。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。

备注

此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void MaxEx4()
{
    Pet[] pets = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };

    int max = pets.Max(pet => pet.Age + pet.Name.Length);

    Console.WriteLine(
        "The maximum pet age plus name length is {0}.",
        max);
}

/*
 This code produces the following output:

 The maximum pet age plus name length is 14.
*/

注解

方法 Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>) 使用 的 Int32IComparable<T> 实现来比较值。

如果提供函数 ,可以将此方法应用于任意值序列, selector该函数将 的成员 source 投影到数值类型中,特别是在 Nullable<Int32> C# 或 Nullable(Of Int32) Visual Basic 中。

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的 Max调用。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

对序列中的每个元素调用转换函数,并返回可以为 null 的最大的 Decimal 值。

C#
public static decimal? Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,decimal?> selector);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最大值的值序列。

selector
Func<TSource,Nullable<Decimal>>

应用于每个元素的转换函数。

返回

C# 或 Nullable(Of Decimal) Visual Basic 中与Nullable<Decimal>序列中的最大值相对应的值。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。

备注

此代码示例使用方法的重载,该方法不同于本文所述的特定重载。 若要将示例扩展到本文所述的重载,请更改函数的 selector 主体。

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void MaxEx4()
{
    Pet[] pets = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };

    int max = pets.Max(pet => pet.Age + pet.Name.Length);

    Console.WriteLine(
        "The maximum pet age plus name length is {0}.",
        max);
}

/*
 This code produces the following output:

 The maximum pet age plus name length is 14.
*/

注解

方法 Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>) 使用 的 DecimalIComparable<T> 实现来比较值。

如果提供函数 ,可以将此方法应用于任意值序列, selector该函数将 的成员 source 投影为数值类型,特别是在 Nullable<Decimal> C# 或 Nullable(Of Decimal) Visual Basic 中。

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的调用 Max

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

对序列中的每个元素调用转换函数,并返回可以为 null 的最大的 Double 值。

C#
public static double? Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,double?> selector);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最大值的值序列。

selector
Func<TSource,Nullable<Double>>

应用于每个元素的转换函数。

返回

C# 或 Nullable(Of Double) Visual Basic 中与Nullable<Double>序列中的最大值相对应的值。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。

备注

此代码示例使用方法的重载,该方法不同于本文所述的特定重载。 若要将示例扩展到本文所述的重载,请更改函数的 selector 主体。

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void MaxEx4()
{
    Pet[] pets = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };

    int max = pets.Max(pet => pet.Age + pet.Name.Length);

    Console.WriteLine(
        "The maximum pet age plus name length is {0}.",
        max);
}

/*
 This code produces the following output:

 The maximum pet age plus name length is 14.
*/

注解

方法 Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>) 使用 的 DoubleIComparable<T> 实现来比较值。

如果提供函数 ,可以将此方法应用于任意值序列, selector该函数将 的成员 source 投影为数值类型,特别是在 Nullable<Double> C# 或 Nullable(Of Double) Visual Basic 中。

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的调用 Max

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

对序列中的每个元素调用转换函数,并返回最大的 Int32 值。

C#
public static int Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int> selector);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最大值的值序列。

selector
Func<TSource,Int32>

应用于每个元素的转换函数。

返回

序列中的最大值。

例外

sourceselectornull

source 中不包含任何元素。

示例

下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void MaxEx4()
{
    Pet[] pets = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };

    int max = pets.Max(pet => pet.Age + pet.Name.Length);

    Console.WriteLine(
        "The maximum pet age plus name length is {0}.",
        max);
}

/*
 This code produces the following output:

 The maximum pet age plus name length is 14.
*/

注解

方法 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 使用 的 Int32IComparable<T> 实现来比较值。

如果提供一个函数 ,selectorsource可将 的成员投影为数值类型(特别是 Int32),则可以将此方法应用于任意值序列。

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的调用 Max

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max<TSource>(IEnumerable<TSource>, Func<TSource,Double>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

对序列中的每个元素调用转换函数,并返回最大的 Double 值。

C#
public static double Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,double> selector);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最大值的值序列。

selector
Func<TSource,Double>

应用于每个元素的转换函数。

返回

序列中的最大值。

例外

sourceselectornull

source 中不包含任何元素。

示例

下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。

备注

此代码示例使用方法的重载,该方法不同于本文所述的特定重载。 若要将示例扩展到本文所述的重载,请更改函数的 selector 主体。

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void MaxEx4()
{
    Pet[] pets = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };

    int max = pets.Max(pet => pet.Age + pet.Name.Length);

    Console.WriteLine(
        "The maximum pet age plus name length is {0}.",
        max);
}

/*
 This code produces the following output:

 The maximum pet age plus name length is 14.
*/

注解

方法 Max<TSource>(IEnumerable<TSource>, Func<TSource,Double>) 使用 的 DoubleIComparable<T> 实现来比较值。

如果提供一个函数 ,selectorsource可将 的成员投影为数值类型(特别是 Double),则可以将此方法应用于任意值序列。

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的调用 Max

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

对序列中的每个元素调用转换函数,并返回最大的 Decimal 值。

C#
public static decimal Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,decimal> selector);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最大值的值序列。

selector
Func<TSource,Decimal>

应用于每个元素的转换函数。

返回

序列中的最大值。

例外

sourceselectornull

source 中不包含任何元素。

示例

下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。

备注

此代码示例使用方法的重载,该方法不同于本文所述的特定重载。 若要将示例扩展到本文所述的重载,请更改函数的 selector 主体。

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void MaxEx4()
{
    Pet[] pets = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };

    int max = pets.Max(pet => pet.Age + pet.Name.Length);

    Console.WriteLine(
        "The maximum pet age plus name length is {0}.",
        max);
}

/*
 This code produces the following output:

 The maximum pet age plus name length is 14.
*/

注解

方法 Max<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>) 使用 的 DecimalIComparable<T> 实现来比较值。

如果提供一个函数 ,selectorsource可将 的成员投影为数值类型(特别是 Decimal),则可以将此方法应用于任意值序列。

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的调用 Max

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max<TSource>(IEnumerable<TSource>, IComparer<TSource>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

返回泛型序列中的最大值。

C#
public static TSource? Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, System.Collections.Generic.IComparer<TSource>? comparer);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最大值的值序列。

comparer
IComparer<TSource>

用于比较值的 IComparer<T>

返回

TSource

序列中的最大值。

例外

sourcenull

source 中没有对象实现 IComparableIComparable<T> 接口。

注解

如果 type TSource 实现 IComparable<T>,则 Max<TSource>(IEnumerable<TSource>) 方法使用该实现来比较值。 否则,如果类型 TSource 实现 IComparable,则使用该实现来比较值。

如果 TSource 是引用类型,并且源序列为空或仅包含值为 null的值,则此方法返回 null

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的调用 Max

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9

Max<TSource>(IEnumerable<TSource>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

返回泛型序列中的最大值。

C#
public static TSource Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
C#
public static TSource? Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最大值的值序列。

返回

TSource

序列中的最大值。

例外

sourcenull

source 中没有对象实现 IComparableIComparable<T> 接口。

示例

下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>) 来确定对象序列 IComparable<T> 中的最大值。

C#
/// <summary>
/// This class implements IComparable to be able to
/// compare one Pet to another Pet.
/// </summary>
class Pet : IComparable<Pet>
{
    public string Name { get; set; }
    public int Age { get; set; }

    /// <summary>
    /// Compares this Pet to another Pet by
    /// summing each Pet's age and name length.
    /// </summary>
    /// <param name="other">The Pet to compare this Pet to.</param>
    /// <returns>-1 if this Pet is 'less' than the other Pet,
    /// 0 if they are equal,
    /// or 1 if this Pet is 'greater' than the other Pet.</returns>
    int IComparable<Pet>.CompareTo(Pet other)
    {
        int sumOther = other.Age + other.Name.Length;
        int sumThis = this.Age + this.Name.Length;

        if (sumOther > sumThis)
            return -1;
        else if (sumOther == sumThis)
            return 0;
        else
            return 1;
    }
}

public static void MaxEx3()
{
    Pet[] pets = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };

    Pet max = pets.Max();

    Console.WriteLine(
        "The 'maximum' animal is {0}.",
        max.Name);
}

/*
 This code produces the following output:

 The 'maximum' animal is Barley.
*/

注解

如果 type TSource 实现 IComparable<T>,则 Max<TSource>(IEnumerable<TSource>) 方法使用该实现来比较值。 否则,如果类型 TSource 实现 IComparable,则使用该实现来比较值。

如果 TSource 是引用类型,并且源序列为空或仅包含值为 null的值,则此方法返回 null

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的调用 Max

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Max<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

对序列中的每个元素调用转换函数,并返回最大的 Int64 值。

C#
public static long Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,long> selector);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最大值的值序列。

selector
Func<TSource,Int64>

应用于每个元素的转换函数。

返回

序列中的最大值。

例外

sourceselectornull

source 中不包含任何元素。

示例

下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。

备注

此代码示例使用方法的重载,该方法不同于本文所述的特定重载。 若要将示例扩展到本文所述的重载,请更改函数的 selector 主体。

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void MaxEx4()
{
    Pet[] pets = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };

    int max = pets.Max(pet => pet.Age + pet.Name.Length);

    Console.WriteLine(
        "The maximum pet age plus name length is {0}.",
        max);
}

/*
 This code produces the following output:

 The maximum pet age plus name length is 14.
*/

注解

方法 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int64>) 使用 的 Int64IComparable<T> 实现来比较值。

如果提供将 的成员source投影为数值类型的函数 selector,则可以将此方法应用于任意值序列,具体来说就是 Int64

在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的 Max调用。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0