Enumerable.Max 方法 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回值序列中的最大值。
重载
Max(IEnumerable<Nullable<Int32>>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
返回可以为 null 的 Int32 值序列中的最大值。
public:
[System::Runtime::CompilerServices::Extension]
 static Nullable<int> Max(System::Collections::Generic::IEnumerable<Nullable<int>> ^ source);public static int? Max (this System.Collections.Generic.IEnumerable<int?> source);static member Max : seq<Nullable<int>> -> Nullable<int><Extension()>
Public Function Max (source As IEnumerable(Of Nullable(Of Integer))) As Nullable(Of Integer)参数
- source
- IEnumerable<Nullable<Int32>>
要确定其最大值的可以为 null 的 Int32 值序列。
返回
C# 或 Nullable(Of Int32) Visual Basic 中类型的Nullable<Int32>值,对应于序列中的最大值。
例外
              source 为 null。
注解
方法 Max(IEnumerable<Nullable<Int32>>) 使用 的 Int32IComparable<T> 实现来比较值。
如果源序列为空或仅包含 的值,则 null此函数返回 null。
在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的调用 Max。
另请参阅
适用于
Max(IEnumerable<Single>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
返回 Single 值序列中的最大值。
public:
[System::Runtime::CompilerServices::Extension]
 static float Max(System::Collections::Generic::IEnumerable<float> ^ source);public static float Max (this System.Collections.Generic.IEnumerable<float> source);static member Max : seq<single> -> single<Extension()>
Public Function Max (source As IEnumerable(Of Single)) As Single参数
- source
- IEnumerable<Single>
要确定其最大值的 Single 值序列。
返回
序列中的最大值。
例外
              source 为 null。
              source 中不包含任何元素。
注解
方法 Max(IEnumerable<Single>) 使用 的 SingleIComparable<T> 实现来比较值。
在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的调用 Max。
另请参阅
适用于
Max(IEnumerable<Nullable<Single>>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
返回可以为 null 的 Single 值序列中的最大值。
public:
[System::Runtime::CompilerServices::Extension]
 static Nullable<float> Max(System::Collections::Generic::IEnumerable<Nullable<float>> ^ source);public static float? Max (this System.Collections.Generic.IEnumerable<float?> source);static member Max : seq<Nullable<single>> -> Nullable<single><Extension()>
Public Function Max (source As IEnumerable(Of Nullable(Of Single))) As Nullable(Of Single)参数
- source
- IEnumerable<Nullable<Single>>
要确定其最大值的可以为 null 的 Single 值序列。
返回
C# 或 Nullable(Of Single) Visual Basic 中类型的Nullable<Single>值,对应于序列中的最大值。
例外
              source 为 null。
注解
方法 Max(IEnumerable<Nullable<Single>>) 使用 的 SingleIComparable<T> 实现来比较值。
如果源序列为空或仅包含 的值,则 null此函数返回 null。
在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的调用 Max。
另请参阅
适用于
Max(IEnumerable<Nullable<Int64>>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
返回可以为 null 的 Int64 值序列中的最大值。
public:
[System::Runtime::CompilerServices::Extension]
 static Nullable<long> Max(System::Collections::Generic::IEnumerable<Nullable<long>> ^ source);public static long? Max (this System.Collections.Generic.IEnumerable<long?> source);static member Max : seq<Nullable<int64>> -> Nullable<int64><Extension()>
Public Function Max (source As IEnumerable(Of Nullable(Of Long))) As Nullable(Of Long)参数
- source
- IEnumerable<Nullable<Int64>>
要确定其最大值的可以为 null 的 Int64 值序列。
返回
C# 或 Nullable(Of Int64) Visual Basic 中类型的Nullable<Int64>值,对应于序列中的最大值。
例外
              source 为 null。
注解
方法 Max(IEnumerable<Nullable<Int64>>) 使用 的 Int64IComparable<T> 实现来比较值。
如果源序列为空或仅包含 的值,则 null此函数返回 null。
在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的调用 Max。
另请参阅
适用于
Max(IEnumerable<Nullable<Double>>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
返回可以为 null 的 Double 值序列中的最大值。
public:
[System::Runtime::CompilerServices::Extension]
 static Nullable<double> Max(System::Collections::Generic::IEnumerable<Nullable<double>> ^ source);public static double? Max (this System.Collections.Generic.IEnumerable<double?> source);static member Max : seq<Nullable<double>> -> Nullable<double><Extension()>
Public Function Max (source As IEnumerable(Of Nullable(Of Double))) As Nullable(Of Double)参数
- source
- IEnumerable<Nullable<Double>>
要确定其最大值的可以为 null 的 Double 值序列。
返回
C# 或 Nullable(Of Double) Visual Basic 中类型的Nullable<Double>值,对应于序列中的最大值。
例外
              source 为 null。
示例
下面的代码示例演示如何使用 Max(IEnumerable<Nullable<Double>>) 来确定序列中的最大值。
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.
*/
' Create an array of Nullable Double values.
Dim doubles() As Nullable(Of Double) =
{Nothing, 1.5E+104, 9.0E+103, -2.0E+103}
' Determine the maximum value in the array.
Dim max As Nullable(Of Double) = doubles.Max()
' Display the result.
Console.WriteLine($"The largest number is {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。
另请参阅
适用于
Max(IEnumerable<Double>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
返回 Double 值序列中的最大值。
public:
[System::Runtime::CompilerServices::Extension]
 static double Max(System::Collections::Generic::IEnumerable<double> ^ source);public static double Max (this System.Collections.Generic.IEnumerable<double> source);static member Max : seq<double> -> double<Extension()>
Public Function Max (source As IEnumerable(Of Double)) As Double参数
- source
- IEnumerable<Double>
要确定其最大值的 Double 值序列。
返回
序列中的最大值。
例外
              source 为 null。
              source 中不包含任何元素。
注解
方法 Max(IEnumerable<Double>) 使用 的 DoubleIComparable<T> 实现来比较值。
在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的调用 Max。
另请参阅
适用于
Max(IEnumerable<Int64>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
返回 Int64 值序列中的最大值。
public:
[System::Runtime::CompilerServices::Extension]
 static long Max(System::Collections::Generic::IEnumerable<long> ^ source);public static long Max (this System.Collections.Generic.IEnumerable<long> source);static member Max : seq<int64> -> int64<Extension()>
Public Function Max (source As IEnumerable(Of Long)) As Long参数
- source
- IEnumerable<Int64>
要确定其最大值的 Int64 值序列。
返回
序列中的最大值。
例外
              source 为 null。
              source 中不包含任何元素。
示例
下面的代码示例演示如何使用 Max(IEnumerable<Int64>) 来确定序列中的最大值。
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.
*/
' Create a list of Long values.
Dim longs As New List(Of Long)(New Long() _
                           {4294967296L, 466855135L, 81125L})
' Get the maximum value in the list.
Dim max As Long = longs.Max()
' Display the result.
Console.WriteLine($"The largest number is {max}")
' This code produces the following output:
'
' The largest number is 4294967296
注解
方法 Max(IEnumerable<Int64>) 使用 的 Int64IComparable<T> 实现来比较值。
在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的 Max调用。
另请参阅
适用于
Max(IEnumerable<Int32>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
返回 Int32 值序列中的最大值。
public:
[System::Runtime::CompilerServices::Extension]
 static int Max(System::Collections::Generic::IEnumerable<int> ^ source);public static int Max (this System.Collections.Generic.IEnumerable<int> source);static member Max : seq<int> -> int<Extension()>
Public Function Max (source As IEnumerable(Of Integer)) As Integer参数
- source
- IEnumerable<Int32>
要确定其最大值的 Int32 值序列。
返回
序列中的最大值。
例外
              source 为 null。
              source 中不包含任何元素。
注解
方法 Max(IEnumerable<Int32>) 使用 的 Int32IComparable<T> 实现来比较值。
在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的 Max调用。
另请参阅
适用于
Max(IEnumerable<Decimal>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
返回 Decimal 值序列中的最大值。
public:
[System::Runtime::CompilerServices::Extension]
 static System::Decimal Max(System::Collections::Generic::IEnumerable<System::Decimal> ^ source);public static decimal Max (this System.Collections.Generic.IEnumerable<decimal> source);static member Max : seq<decimal> -> decimal<Extension()>
Public Function Max (source As IEnumerable(Of Decimal)) As Decimal参数
- source
- IEnumerable<Decimal>
要确定其最大值的 Decimal 值序列。
返回
序列中的最大值。
例外
              source 为 null。
              source 中不包含任何元素。
注解
方法 Max(IEnumerable<Decimal>) 使用 的 DecimalIComparable<T> 实现来比较值。
在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的 Max调用。
另请参阅
适用于
Max(IEnumerable<Nullable<Decimal>>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
返回可以为 null 的 Decimal 值序列中的最大值。
public:
[System::Runtime::CompilerServices::Extension]
 static Nullable<System::Decimal> Max(System::Collections::Generic::IEnumerable<Nullable<System::Decimal>> ^ source);public static decimal? Max (this System.Collections.Generic.IEnumerable<decimal?> source);static member Max : seq<Nullable<decimal>> -> Nullable<decimal><Extension()>
Public Function Max (source As IEnumerable(Of Nullable(Of Decimal))) As Nullable(Of Decimal)参数
- source
- IEnumerable<Nullable<Decimal>>
要确定其最大值的可以为 null 的 Decimal 值序列。
返回
C# 或 Nullable(Of Decimal) Visual Basic 中的类型Nullable<Decimal>值,对应于序列中的最大值。
例外
              source 为 null。
注解
方法 Max(IEnumerable<Nullable<Decimal>>) 使用 的 DecimalIComparable<T> 实现来比较值。
如果源序列为空或仅包含为 null的值,则此函数返回 null。
在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的 Max调用。
另请参阅
适用于
Max<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
对序列中的每个元素调用转换函数,并返回最大结果值。
public:
generic <typename TSource, typename TResult>
[System::Runtime::CompilerServices::Extension]
 static TResult Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TResult> ^ selector);public static TResult Max<TSource,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TResult> selector);public static TResult? Max<TSource,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TResult> selector);static member Max : seq<'Source> * Func<'Source, 'Result> -> 'Result<Extension()>
Public Function Max(Of TSource, TResult) (source As IEnumerable(Of TSource), selector As Func(Of TSource, TResult)) As TResult类型参数
- TSource
              source 的元素类型。
- TResult
              selector 返回的值的类型。
参数
- source
- IEnumerable<TSource>
要确定其最大值的值序列。
- selector
- Func<TSource,TResult>
应用于每个元素的转换函数。
返回
序列中的最大值。
例外
              source 或 selector 为 null。
示例
下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。
注意
此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。
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.
*/
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure
Sub MaxEx4()
    ' Create an array of Pet objects.
    Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8},
                     New Pet With {.Name = "Boots", .Age = 4},
                     New Pet With {.Name = "Whiskers", .Age = 1}}
    ' Determine the "maximum" pet by passing a
    ' lambda expression to Max() that sums the pet's age
    ' and name length.
    Dim max As Integer = pets.Max(Function(pet) _
                                  pet.Age + pet.Name.Length)
    ' Display the result.
    Console.WriteLine($"The maximum pet age plus name length is {max}")
End Sub
' 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调用。
另请参阅
适用于
Max<TSource>(IEnumerable<TSource>, Func<TSource,Single>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
对序列中的每个元素调用转换函数,并返回最大的 Single 值。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static float Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, float> ^ selector);public static float Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,float> selector);static member Max : seq<'Source> * Func<'Source, single> -> single<Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Single)) As Single类型参数
- TSource
              source 的元素类型。
参数
- source
- IEnumerable<TSource>
要确定其最大值的值序列。
返回
序列中的最大值。
例外
              source 或 selector 为 null。
              source 中不包含任何元素。
示例
下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。
注意
此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。
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.
*/
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure
Sub MaxEx4()
    ' Create an array of Pet objects.
    Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8},
                     New Pet With {.Name = "Boots", .Age = 4},
                     New Pet With {.Name = "Whiskers", .Age = 1}}
    ' Determine the "maximum" pet by passing a
    ' lambda expression to Max() that sums the pet's age
    ' and name length.
    Dim max As Integer = pets.Max(Function(pet) _
                                  pet.Age + pet.Name.Length)
    ' Display the result.
    Console.WriteLine($"The maximum pet age plus name length is {max}")
End Sub
' 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调用。
另请参阅
适用于
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
对序列中的每个元素调用转换函数,并返回可以为 null 的最大的 Single 值。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static Nullable<float> Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, Nullable<float>> ^ selector);public static float? Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,float?> selector);static member Max : seq<'Source> * Func<'Source, Nullable<single>> -> Nullable<single><Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Nullable(Of Single))) As Nullable(Of Single)类型参数
- TSource
              source 的元素类型。
参数
- source
- IEnumerable<TSource>
要确定其最大值的值序列。
返回
对应于序列中最大值的值。
例外
              source 或 selector 为 null。
示例
下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。
注意
此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。
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.
*/
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure
Sub MaxEx4()
    ' Create an array of Pet objects.
    Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8},
                     New Pet With {.Name = "Boots", .Age = 4},
                     New Pet With {.Name = "Whiskers", .Age = 1}}
    ' Determine the "maximum" pet by passing a
    ' lambda expression to Max() that sums the pet's age
    ' and name length.
    Dim max As Integer = pets.Max(Function(pet) _
                                  pet.Age + pet.Name.Length)
    ' Display the result.
    Console.WriteLine($"The maximum pet age plus name length is {max}")
End Sub
' 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调用。
另请参阅
适用于
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
对序列中的每个元素调用转换函数,并返回可以为 null 的最大的 Int64 值。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static Nullable<long> Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, Nullable<long>> ^ selector);public static long? Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,long?> selector);static member Max : seq<'Source> * Func<'Source, Nullable<int64>> -> Nullable<int64><Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Nullable(Of Long))) As Nullable(Of Long)类型参数
- TSource
              source 的元素类型。
参数
- source
- IEnumerable<TSource>
要确定其最大值的值序列。
返回
对应于序列中最大值的值。
例外
              source 或 selector 为 null。
示例
下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。
注意
此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。
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.
*/
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure
Sub MaxEx4()
    ' Create an array of Pet objects.
    Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8},
                     New Pet With {.Name = "Boots", .Age = 4},
                     New Pet With {.Name = "Whiskers", .Age = 1}}
    ' Determine the "maximum" pet by passing a
    ' lambda expression to Max() that sums the pet's age
    ' and name length.
    Dim max As Integer = pets.Max(Function(pet) _
                                  pet.Age + pet.Name.Length)
    ' Display the result.
    Console.WriteLine($"The maximum pet age plus name length is {max}")
End Sub
' 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调用。
另请参阅
适用于
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
对序列中的每个元素调用转换函数,并返回可以为 null 的最大的 Int32 值。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static Nullable<int> Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, Nullable<int>> ^ selector);public static int? Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int?> selector);static member Max : seq<'Source> * Func<'Source, Nullable<int>> -> Nullable<int><Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Nullable(Of Integer))) As Nullable(Of Integer)类型参数
- TSource
              source 的元素类型。
参数
- source
- IEnumerable<TSource>
要确定其最大值的值序列。
返回
C# 中或 Nullable(Of Int32) Visual Basic 中与序列中的最大值相对应的类型Nullable<Int32>的值。
例外
              source 或 selector 为 null。
示例
下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。
注意
此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。
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.
*/
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure
Sub MaxEx4()
    ' Create an array of Pet objects.
    Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8},
                     New Pet With {.Name = "Boots", .Age = 4},
                     New Pet With {.Name = "Whiskers", .Age = 1}}
    ' Determine the "maximum" pet by passing a
    ' lambda expression to Max() that sums the pet's age
    ' and name length.
    Dim max As Integer = pets.Max(Function(pet) _
                                  pet.Age + pet.Name.Length)
    ' Display the result.
    Console.WriteLine($"The maximum pet age plus name length is {max}")
End Sub
' 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调用。
另请参阅
适用于
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
对序列中的每个元素调用转换函数,并返回可以为 null 的最大的 Decimal 值。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static Nullable<System::Decimal> Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, Nullable<System::Decimal>> ^ selector);public static decimal? Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,decimal?> selector);static member Max : seq<'Source> * Func<'Source, Nullable<decimal>> -> Nullable<decimal><Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Nullable(Of Decimal))) As Nullable(Of Decimal)类型参数
- TSource
              source 的元素类型。
参数
- source
- IEnumerable<TSource>
要确定其最大值的值序列。
返回
C# 或 Nullable(Of Decimal) Visual Basic 中与Nullable<Decimal>序列中的最大值相对应的值。
例外
              source 或 selector 为 null。
示例
下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。
注意
此代码示例使用方法的重载,该方法不同于本文所述的特定重载。 若要将示例扩展到本文所述的重载,请更改函数的 selector 主体。
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.
*/
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure
Sub MaxEx4()
    ' Create an array of Pet objects.
    Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8},
                     New Pet With {.Name = "Boots", .Age = 4},
                     New Pet With {.Name = "Whiskers", .Age = 1}}
    ' Determine the "maximum" pet by passing a
    ' lambda expression to Max() that sums the pet's age
    ' and name length.
    Dim max As Integer = pets.Max(Function(pet) _
                                  pet.Age + pet.Name.Length)
    ' Display the result.
    Console.WriteLine($"The maximum pet age plus name length is {max}")
End Sub
' 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。
另请参阅
适用于
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
对序列中的每个元素调用转换函数,并返回可以为 null 的最大的 Double 值。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static Nullable<double> Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, Nullable<double>> ^ selector);public static double? Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,double?> selector);static member Max : seq<'Source> * Func<'Source, Nullable<double>> -> Nullable<double><Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Nullable(Of Double))) As Nullable(Of Double)类型参数
- TSource
              source 的元素类型。
参数
- source
- IEnumerable<TSource>
要确定其最大值的值序列。
返回
C# 或 Nullable(Of Double) Visual Basic 中与Nullable<Double>序列中的最大值相对应的值。
例外
              source 或 selector 为 null。
示例
下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。
注意
此代码示例使用方法的重载,该方法不同于本文所述的特定重载。 若要将示例扩展到本文所述的重载,请更改函数的 selector 主体。
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.
*/
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure
Sub MaxEx4()
    ' Create an array of Pet objects.
    Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8},
                     New Pet With {.Name = "Boots", .Age = 4},
                     New Pet With {.Name = "Whiskers", .Age = 1}}
    ' Determine the "maximum" pet by passing a
    ' lambda expression to Max() that sums the pet's age
    ' and name length.
    Dim max As Integer = pets.Max(Function(pet) _
                                  pet.Age + pet.Name.Length)
    ' Display the result.
    Console.WriteLine($"The maximum pet age plus name length is {max}")
End Sub
' 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。
另请参阅
适用于
Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
对序列中的每个元素调用转换函数,并返回最大的 Int32 值。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static int Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, int> ^ selector);public static int Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int> selector);static member Max : seq<'Source> * Func<'Source, int> -> int<Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Integer)) As Integer类型参数
- TSource
              source 的元素类型。
参数
- source
- IEnumerable<TSource>
要确定其最大值的值序列。
返回
序列中的最大值。
例外
              source 或 selector 为 null。
              source 中不包含任何元素。
示例
下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。
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.
*/
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure
Sub MaxEx4()
    ' Create an array of Pet objects.
    Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8},
                     New Pet With {.Name = "Boots", .Age = 4},
                     New Pet With {.Name = "Whiskers", .Age = 1}}
    ' Determine the "maximum" pet by passing a
    ' lambda expression to Max() that sums the pet's age
    ' and name length.
    Dim max As Integer = pets.Max(Function(pet) _
                                  pet.Age + pet.Name.Length)
    ' Display the result.
    Console.WriteLine($"The maximum pet age plus name length is {max}")
End Sub
' 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。
另请参阅
适用于
Max<TSource>(IEnumerable<TSource>, Func<TSource,Double>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
对序列中的每个元素调用转换函数,并返回最大的 Double 值。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static double Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, double> ^ selector);public static double Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,double> selector);static member Max : seq<'Source> * Func<'Source, double> -> double<Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Double)) As Double类型参数
- TSource
              source 的元素类型。
参数
- source
- IEnumerable<TSource>
要确定其最大值的值序列。
返回
序列中的最大值。
例外
              source 或 selector 为 null。
              source 中不包含任何元素。
示例
下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。
注意
此代码示例使用方法的重载,该方法不同于本文所述的特定重载。 若要将示例扩展到本文所述的重载,请更改函数的 selector 主体。
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.
*/
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure
Sub MaxEx4()
    ' Create an array of Pet objects.
    Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8},
                     New Pet With {.Name = "Boots", .Age = 4},
                     New Pet With {.Name = "Whiskers", .Age = 1}}
    ' Determine the "maximum" pet by passing a
    ' lambda expression to Max() that sums the pet's age
    ' and name length.
    Dim max As Integer = pets.Max(Function(pet) _
                                  pet.Age + pet.Name.Length)
    ' Display the result.
    Console.WriteLine($"The maximum pet age plus name length is {max}")
End Sub
' 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。
另请参阅
适用于
Max<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
对序列中的每个元素调用转换函数,并返回最大的 Decimal 值。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Decimal Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, System::Decimal> ^ selector);public static decimal Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,decimal> selector);static member Max : seq<'Source> * Func<'Source, decimal> -> decimal<Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Decimal)) As Decimal类型参数
- TSource
              source 的元素类型。
参数
- source
- IEnumerable<TSource>
要确定其最大值的值序列。
返回
序列中的最大值。
例外
              source 或 selector 为 null。
              source 中不包含任何元素。
示例
下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。
注意
此代码示例使用方法的重载,该方法不同于本文所述的特定重载。 若要将示例扩展到本文所述的重载,请更改函数的 selector 主体。
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.
*/
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure
Sub MaxEx4()
    ' Create an array of Pet objects.
    Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8},
                     New Pet With {.Name = "Boots", .Age = 4},
                     New Pet With {.Name = "Whiskers", .Age = 1}}
    ' Determine the "maximum" pet by passing a
    ' lambda expression to Max() that sums the pet's age
    ' and name length.
    Dim max As Integer = pets.Max(Function(pet) _
                                  pet.Age + pet.Name.Length)
    ' Display the result.
    Console.WriteLine($"The maximum pet age plus name length is {max}")
End Sub
' 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。
另请参阅
适用于
Max<TSource>(IEnumerable<TSource>, IComparer<TSource>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
返回泛型序列中的最大值。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource Max(System::Collections::Generic::IEnumerable<TSource> ^ source, System::Collections::Generic::IComparer<TSource> ^ comparer);public static TSource? Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, System.Collections.Generic.IComparer<TSource>? comparer);static member Max : seq<'Source> * System.Collections.Generic.IComparer<'Source> -> 'Source<Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), comparer As IComparer(Of TSource)) As TSource类型参数
- TSource
              source 的元素类型。
参数
- source
- IEnumerable<TSource>
要确定其最大值的值序列。
- comparer
- IComparer<TSource>
用于比较值的 IComparer<T>。
返回
序列中的最大值。
例外
              source 为 null。
              source 中没有对象实现 IComparable 或 IComparable<T> 接口。
注解
如果 type TSource 实现 IComparable<T>,则 Max<TSource>(IEnumerable<TSource>) 方法使用该实现来比较值。 否则,如果类型 TSource 实现 IComparable,则使用该实现来比较值。
如果 TSource 是引用类型,并且源序列为空或仅包含值为 null的值,则此方法返回 null。
在 Visual Basic 查询表达式语法中, Aggregate Into Max() 子句转换为 的调用 Max。
适用于
Max<TSource>(IEnumerable<TSource>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
返回泛型序列中的最大值。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource Max(System::Collections::Generic::IEnumerable<TSource> ^ source);public static TSource Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);public static TSource? Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);static member Max : seq<'Source> -> 'Source<Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource)) As TSource类型参数
- TSource
              source 的元素类型。
参数
- source
- IEnumerable<TSource>
要确定其最大值的值序列。
返回
序列中的最大值。
例外
              source 为 null。
              source 中没有对象实现 IComparable 或 IComparable<T> 接口。
示例
下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>) 来确定对象序列 IComparable<T> 中的最大值。
/// <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.
*/
' This class implements IComparable
' and has a custom 'CompareTo' implementation.
Class Pet
    Implements IComparable(Of Pet)
    Public Name As String
    Public Age As Integer
    ''' <summary>
    ''' Compares Pet objects by the sum of their age and name length.
    ''' </summary>
    ''' <param name="other">The Pet to compare this Pet to.</param>
    ''' <returns>-1 if this Pet's sum is 'less' than the other Pet,
    ''' 0 if they are equal,
    ''' or 1 if this Pet's sum is 'greater' than the other Pet.</returns>
    Function CompareTo(ByVal other As Pet) As Integer _
    Implements IComparable(Of Pet).CompareTo
        If (other.Age + other.Name.Length > Me.Age + Me.Name.Length) Then
            Return -1
        ElseIf (other.Age + other.Name.Length = Me.Age + Me.Name.Length) Then
            Return 0
        Else
            Return 1
        End If
    End Function
End Class
Sub MaxEx3()
    ' Create an array of Pet objects.
    Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8},
                     New Pet With {.Name = "Boots", .Age = 4},
                     New Pet With {.Name = "Whiskers", .Age = 1}}
    ' Find the "maximum" pet according to the
    ' custom CompareTo() implementation.
    Dim max As Pet = pets.Max()
    ' Display the result.
    Console.WriteLine($"The 'maximum' animal is {max.Name}")
End Sub
' 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。
另请参阅
适用于
Max<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)
- Source:
- Max.cs
- Source:
- Max.cs
- Source:
- Max.cs
对序列中的每个元素调用转换函数,并返回最大的 Int64 值。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static long Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, long> ^ selector);public static long Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,long> selector);static member Max : seq<'Source> * Func<'Source, int64> -> int64<Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Long)) As Long类型参数
- TSource
              source 的元素类型。
参数
- source
- IEnumerable<TSource>
要确定其最大值的值序列。
返回
序列中的最大值。
例外
              source 或 selector 为 null。
              source 中不包含任何元素。
示例
下面的代码示例演示如何使用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最大值。
注意
此代码示例使用方法的重载,该方法不同于本文所述的特定重载。 若要将示例扩展到本文所述的重载,请更改函数的 selector 主体。
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.
*/
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure
Sub MaxEx4()
    ' Create an array of Pet objects.
    Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8},
                     New Pet With {.Name = "Boots", .Age = 4},
                     New Pet With {.Name = "Whiskers", .Age = 1}}
    ' Determine the "maximum" pet by passing a
    ' lambda expression to Max() that sums the pet's age
    ' and name length.
    Dim max As Integer = pets.Max(Function(pet) _
                                  pet.Age + pet.Name.Length)
    ' Display the result.
    Console.WriteLine($"The maximum pet age plus name length is {max}")
End Sub
' 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调用。