Enumerable.Max 方法

定義

回傳一連串值的最大值。

多載

名稱 Description
Max(IEnumerable<Nullable<Int64>>)

回傳一串可 Int64 空值序列中的最大值。

Max(IEnumerable<Nullable<Int32>>)

回傳一串可 Int32 空值序列中的最大值。

Max(IEnumerable<Nullable<Double>>)

回傳一串可 Double 空值序列中的最大值。

Max(IEnumerable<Nullable<Decimal>>)

回傳一串可 Decimal 空值序列中的最大值。

Max(IEnumerable<Double>)

回傳一連串 Double 值的最大值。

Max(IEnumerable<Int32>)

回傳一連串 Int32 值的最大值。

Max(IEnumerable<Decimal>)

回傳一連串 Decimal 值的最大值。

Max(IEnumerable<Nullable<Single>>)

回傳一串可 Single 空值序列中的最大值。

Max(IEnumerable<Int64>)

回傳一連串 Int64 值的最大值。

Max(IEnumerable<Single>)

回傳一連串 Single 值的最大值。

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

在泛型序列的每個專案上叫用轉換函式,並傳回產生的最大值。

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

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

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

在序列的每個元素上叫用轉換函式,並傳回最大 Decimal 值。

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

在序列的每個元素上叫用轉換函式,並傳回最大 Double 值。

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

在序列的每個元素上叫用轉換函式,並傳回最大 Int32 值。

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

在序列的每個元素上叫用轉換函式,並傳回最大 Int64 值。

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,Nullable<Int32>>)

在序列的每個元素上叫用轉換函式,並傳回可為 Null 的最大 Int32 值。

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

在序列的每個元素上叫用轉換函式,並傳回可為 Null 的最大 Int64 值。

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

在序列的每個元素上叫用轉換函式,並傳回可為 Null 的最大 Single 值。

Max<TSource>(IEnumerable<TSource>)

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

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

在序列的每個元素上叫用轉換函式,並傳回最大 Single 值。

Max(IEnumerable<Nullable<Int64>>)

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs

回傳一串可 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>>

一連串可 Int64 為空值,用以決定最大值。

傳回

C# 中為 Nullable<Int64> 或 Visual Basic 中為 Nullable(Of Int64) 的值,對應序列中的最大值。

例外狀況

sourcenull

備註

Max(IEnumerable<Nullable<Int64>>) 方法利用 Int64IComparable<T> 實作來比較數值。

若來源序列為空或僅包含 的 null值,該函數會回傳 null

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

另請參閱

適用於

Max(IEnumerable<Nullable<Int32>>)

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs

回傳一串可 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>>

一連串可 Int32 為空值,用以決定最大值。

傳回

C# 中為 Nullable<Int32> 或 Visual Basic 中為 Nullable(Of Int32) 的值,對應序列中的最大值。

例外狀況

sourcenull

備註

Max(IEnumerable<Nullable<Int32>>) 方法利用 Int32IComparable<T> 實作來比較數值。

若來源序列為空或僅包含 的 null值,該函數會回傳 null

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

另請參閱

適用於

Max(IEnumerable<Nullable<Double>>)

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs

回傳一串可 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>>

一連串可 Double 為空值,用以決定最大值。

傳回

C# 中為 Nullable<Double> 或 Visual Basic 中為 Nullable(Of Double) 的值,對應序列中的最大值。

例外狀況

sourcenull

範例

以下程式碼範例示範如何用 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<Nullable<Decimal>>)

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs

回傳一串可 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>>

一連串可 Decimal 為空值,用以決定最大值。

傳回

C# 中為 Nullable<Decimal> 或 Visual Basic 中為 Nullable(Of Decimal) 的值,對應序列中的最大值。

例外狀況

sourcenull

備註

Max(IEnumerable<Nullable<Decimal>>) 方法利用 DecimalIComparable<T> 實作來比較數值。

若來源序列為空或僅包含 的 null值,該函數會回傳 null

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

另請參閱

適用於

Max(IEnumerable<Double>)

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
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 的數值用來決定最大值。

傳回

序列中的最大值。

例外狀況

sourcenull

source 不含任何元素。

備註

Max(IEnumerable<Double>) 方法利用 DoubleIComparable<T> 實作來比較數值。

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

另請參閱

適用於

Max(IEnumerable<Int32>)

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
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 的數值用來決定最大值。

傳回

序列中的最大值。

例外狀況

sourcenull

source 不含任何元素。

備註

Max(IEnumerable<Int32>) 方法利用 Int32IComparable<T> 實作來比較數值。

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

另請參閱

適用於

Max(IEnumerable<Decimal>)

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
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 的數值用來決定最大值。

傳回

序列中的最大值。

例外狀況

sourcenull

source 不含任何元素。

備註

Max(IEnumerable<Decimal>) 方法利用 DecimalIComparable<T> 實作來比較數值。

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

另請參閱

適用於

Max(IEnumerable<Nullable<Single>>)

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs

回傳一串可 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>>

一連串可 Single 為空值,用以決定最大值。

傳回

C# 中為 Nullable<Single> 或 Visual Basic 中為 Nullable(Of Single) 的值,對應序列中的最大值。

例外狀況

sourcenull

備註

Max(IEnumerable<Nullable<Single>>) 方法利用 SingleIComparable<T> 實作來比較數值。

若來源序列為空或僅包含 的 null值,該函數會回傳 null

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

另請參閱

適用於

Max(IEnumerable<Int64>)

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
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 的數值用來決定最大值。

傳回

序列中的最大值。

例外狀況

sourcenull

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<Single>)

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
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 的數值用來決定最大值。

傳回

序列中的最大值。

例外狀況

sourcenull

source 不含任何元素。

備註

Max(IEnumerable<Single>) 方法利用 SingleIComparable<T> 實作來比較數值。

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

另請參閱

適用於

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

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
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>

一個轉換函數,可以套用到每個元素。

傳回

TResult

序列中的最大值。

例外狀況

sourceselectornull

範例

以下程式碼範例示範如何用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來決定一連串投影值的最大值。

Note

這個程式碼範例使用的方法的超載,與本文描述的具體超載不同。 若要將範例延伸到本文描述的過載,請改變函數的主體 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

備註

若型別 TResultIComparable<T>作 ,此方法利用該實作來比較值。 否則,若型別 TResultIComparable作 ,則該實作用於比較值。

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

另請參閱

適用於

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

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
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> 價值。

傳回

TSource

序列中的最大值。

例外狀況

sourcenull

沒有任何 source 物件實 IComparable 作 OR IComparable<T> 介面。

備註

若型別 TSourceIComparable<T>作 ,該 Max<TSource>(IEnumerable<TSource>) 方法會利用該實作來比較值。 否則,若型別 TSourceIComparable作 ,則該實作用於比較值。

TSource 是參考型別且來源序列為空或僅包含 , null此方法會回傳 null

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

適用於

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

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
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>

一連串的數值用來決定最大值。

selector
Func<TSource,Decimal>

一個轉換函數,可以套用到每個元素。

傳回

序列中的最大值。

例外狀況

sourceselectornull

source 不含任何元素。

範例

以下程式碼範例示範如何用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來決定一連串投影值的最大值。

Note

這個程式碼範例使用的方法的超載,與本文描述的具體超載不同。 若要將範例延伸到本文描述的過載,請改變函數的主體 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> 實作來比較數值。

如果你提供一個函數 selector,將 的 source 成員投影成一個數值型別,具體來說 Decimal,你可以將此方法應用於任意值序列。

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

另請參閱

適用於

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

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
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>

一連串的數值用來決定最大值。

selector
Func<TSource,Double>

一個轉換函數,可以套用到每個元素。

傳回

序列中的最大值。

例外狀況

sourceselectornull

source 不含任何元素。

範例

以下程式碼範例示範如何用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來決定一連串投影值的最大值。

Note

這個程式碼範例使用的方法的超載,與本文描述的具體超載不同。 若要將範例延伸到本文描述的過載,請改變函數的主體 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> 實作來比較數值。

如果你提供一個函數 selector,將 的 source 成員投影成一個數值型別,具體來說 Double,你可以將此方法應用於任意值序列。

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

另請參閱

適用於

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

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
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>

一連串的數值用來決定最大值。

selector
Func<TSource,Int32>

一個轉換函數,可以套用到每個元素。

傳回

序列中的最大值。

例外狀況

sourceselectornull

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> 實作來比較數值。

如果你提供一個函數 selector,將 的 source 成員投影成一個數值型別,具體來說 Int32,你可以將此方法應用於任意值序列。

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

另請參閱

適用於

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

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
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>

一連串的數值用來決定最大值。

selector
Func<TSource,Int64>

一個轉換函數,可以套用到每個元素。

傳回

序列中的最大值。

例外狀況

sourceselectornull

source 不含任何元素。

範例

以下程式碼範例示範如何用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來決定一連串投影值的最大值。

Note

這個程式碼範例使用的方法的超載,與本文描述的具體超載不同。 若要將範例延伸到本文描述的過載,請改變函數的主體 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> 實作來比較數值。

如果你提供一個函數 selector,將 的 source 成員投影成一個數值型別,具體來說 Int64,你可以將此方法應用於任意值序列。

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

另請參閱

適用於

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

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
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>

一連串的數值用來決定最大值。

selector
Func<TSource,Nullable<Decimal>>

一個轉換函數,可以套用到每個元素。

傳回

C# 中的 Nullable<Decimal> 或 Visual Basic 中的 Nullable(Of Decimal) 對應序列的最大值。

例外狀況

sourceselectornull

範例

以下程式碼範例示範如何用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來決定一連串投影值的最大值。

Note

這個程式碼範例使用的方法的超載,與本文描述的具體超載不同。 若要將範例延伸到本文描述的過載,請改變函數的主體 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 的成員投影成數值型態,例如在 C# 中的 Nullable<Decimal> 或在 Visual Basic 中的 Nullable(Of Decimal),就可以將此方法應用於任意值序列。

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

另請參閱

適用於

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

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
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>

一連串的數值用來決定最大值。

selector
Func<TSource,Nullable<Double>>

一個轉換函數,可以套用到每個元素。

傳回

C# 中的 Nullable<Double> 或 Visual Basic 中的 Nullable(Of Double) 對應序列的最大值。

例外狀況

sourceselectornull

範例

以下程式碼範例示範如何用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來決定一連串投影值的最大值。

Note

這個程式碼範例使用的方法的超載,與本文描述的具體超載不同。 若要將範例延伸到本文描述的過載,請改變函數的主體 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 的成員投影成數值型態,例如在 C# 中的 Nullable<Double> 或在 Visual Basic 中的 Nullable(Of Double),就可以將此方法應用於任意值序列。

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

另請參閱

適用於

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

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
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>

一連串的數值用來決定最大值。

selector
Func<TSource,Nullable<Int32>>

一個轉換函數,可以套用到每個元素。

傳回

C# 中的 Nullable<Int32> 或 Visual Basic 中的 Nullable(Of Int32) 對應序列的最大值。

例外狀況

sourceselectornull

範例

以下程式碼範例示範如何用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來決定一連串投影值的最大值。

Note

這個程式碼範例使用的方法的超載,與本文描述的具體超載不同。 若要將範例延伸到本文描述的過載,請改變函數的主體 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 的成員投影成數值型態,例如在 C# 中的 Nullable<Int32> 或在 Visual Basic 中的 Nullable(Of Int32),就可以將此方法應用於任意值序列。

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

另請參閱

適用於

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

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
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>

一連串的數值用來決定最大值。

selector
Func<TSource,Nullable<Int64>>

一個轉換函數,可以套用到每個元素。

傳回

對應於序列中最大值的值。

例外狀況

sourceselectornull

範例

以下程式碼範例示範如何用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來決定一連串投影值的最大值。

Note

這個程式碼範例使用的方法的超載,與本文描述的具體超載不同。 若要將範例延伸到本文描述的過載,請改變函數的主體 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 的成員投影成數值型態,例如在 C# 中的 Nullable<Int64> 或在 Visual Basic 中的 Nullable(Of Int64),就可以將此方法應用於任意值序列。

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

另請參閱

適用於

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

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
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>

一連串的數值用來決定最大值。

selector
Func<TSource,Nullable<Single>>

一個轉換函數,可以套用到每個元素。

傳回

對應於序列中最大值的值。

例外狀況

sourceselectornull

範例

以下程式碼範例示範如何用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來決定一連串投影值的最大值。

Note

這個程式碼範例使用的方法的超載,與本文描述的具體超載不同。 若要將範例延伸到本文描述的過載,請改變函數的主體 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 的成員投影成數值型態,例如在 C# 中的 Nullable<Single> 或在 Visual Basic 中的 Nullable(Of Single),就可以將此方法應用於任意值序列。

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

另請參閱

適用於

Max<TSource>(IEnumerable<TSource>)

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
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>

一連串的數值用來決定最大值。

傳回

TSource

序列中的最大值。

例外狀況

sourcenull

沒有任何 source 物件實 IComparable 作 OR 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

備註

若型別 TSourceIComparable<T>作 ,該 Max<TSource>(IEnumerable<TSource>) 方法會利用該實作來比較值。 否則,若型別 TSourceIComparable作 ,則該實作用於比較值。

TSource 是參考型別且來源序列為空或僅包含 , null此方法會回傳 null

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

另請參閱

適用於

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

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
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>

一連串的數值用來決定最大值。

selector
Func<TSource,Single>

一個轉換函數,可以套用到每個元素。

傳回

序列中的最大值。

例外狀況

sourceselectornull

source 不含任何元素。

範例

以下程式碼範例示範如何用 Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來決定一連串投影值的最大值。

Note

這個程式碼範例使用的方法的超載,與本文描述的具體超載不同。 若要將範例延伸到本文描述的過載,請改變函數的主體 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> 實作來比較數值。

如果你提供一個函數 selector,將 的 source 成員投影成一個數值型別,具體來說 Single,你可以將此方法應用於任意值序列。

在Visual Basic查詢表達式語法中,Aggregate Into Max()子句可轉譯為Max的呼叫。

另請參閱

適用於