Enumerable.Min 方法

定義

傳回值序列中的最小值。

多載

Min(IEnumerable<Nullable<Int32>>)

傳回可為 Null 之 Int32 值序列中的最小值。

Min(IEnumerable<Single>)

傳回 Single 值序列中的最小值。

Min(IEnumerable<Nullable<Single>>)

傳回可為 Null 之 Single 值序列中的最小值。

Min(IEnumerable<Nullable<Int64>>)

傳回可為 Null 之 Int64 值序列中的最小值。

Min(IEnumerable<Nullable<Double>>)

傳回可為 Null 之 Double 值序列中的最小值。

Min(IEnumerable<Decimal>)

傳回 Decimal 值序列中的最小值。

Min(IEnumerable<Int64>)

傳回 Int64 值序列中的最小值。

Min(IEnumerable<Int32>)

傳回 Int32 值序列中的最小值。

Min(IEnumerable<Double>)

傳回 Double 值序列中的最小值。

Min(IEnumerable<Nullable<Decimal>>)

傳回可為 Null 之 Decimal 值序列中的最小值。

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

在泛型序列的每個項目上叫用轉換函式,並傳回最小的結果值。

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

在序列的每個項目上叫用轉換函式,並傳回最小的 Single 值。

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

在序列的每個項目上叫用轉換函式,並傳回最小的可為 Null 之 Single 值。

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

在序列的每個項目上叫用轉換函式,並傳回最小的可為 Null 之 Int64 值。

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

在序列的每個項目上叫用轉換函式,並傳回最小的可為 Null 之 Double 值。

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

在序列的每個項目上叫用轉換函式,並傳回最小的可為 Null 之 Decimal 值。

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

在序列的每個項目上叫用轉換函式,並傳回最小的可為 Null 之 Int32 值。

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

在序列的每個項目上叫用轉換函式,並傳回最小的 Int32 值。

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

在序列的每個項目上叫用轉換函式,並傳回最小的 Double 值。

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

在序列的每個項目上叫用轉換函式,並傳回最小的 Decimal 值。

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

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

Min<TSource>(IEnumerable<TSource>)

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

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

在序列的每個項目上叫用轉換函式,並傳回最小的 Int64 值。

Min(IEnumerable<Nullable<Int32>>)

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

傳回可為 Null 之 Int32 值序列中的最小值。

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

參數

source
IEnumerable<Nullable<Int32>>

要判斷最小值之可為 Null 的 Int32 值序列。

傳回

對應至序列中最小值的值。

例外狀況

sourcenull

範例

下列程式代碼範例示範如何使用 Min(IEnumerable<Nullable<Int32>>) 來判斷序列中的最小值。

C#
int?[] grades = { 78, 92, null, 99, 37, 81 };

int? min = grades.Min();

Console.WriteLine("The lowest grade is {0}.", min);

/*
 This code produces the following output:

 The lowest grade is 37.
*/

備註

方法Min(IEnumerable<Nullable<Int32>>)會使用的 IComparable<T> 實作Int32來比較值。

如果來源序列是空的,或只包含的值 null,則此函式會傳 null回 。

在 Visual Basic 查詢表達式語法中 Aggregate Into Min() ,子句會轉譯為的 Min調用。

另請參閱

適用於

.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

Min(IEnumerable<Single>)

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

傳回 Single 值序列中的最小值。

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

參數

source
IEnumerable<Single>

要判斷最小值的 Single 值序列。

傳回

序列中的最小值。

例外狀況

sourcenull

source 沒有包含任何項目。

備註

方法Min(IEnumerable<Single>)會使用的 IComparable<T> 實作Single來比較值。

在 Visual Basic 查詢表達式語法中 Aggregate Into Min() ,子句會轉譯為的 Min調用。

另請參閱

適用於

.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

Min(IEnumerable<Nullable<Single>>)

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

傳回可為 Null 之 Single 值序列中的最小值。

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

參數

source
IEnumerable<Nullable<Single>>

要判斷最小值之可為 Null 的 Single 值序列。

傳回

對應至序列中最小值的值。

例外狀況

sourcenull

備註

方法Min(IEnumerable<Nullable<Single>>)會使用的 IComparable<T> 實作Single來比較值。

如果來源序列是空的,或只包含的值 null,則此函式會傳 null回 。

在 Visual Basic 查詢表達式語法中 Aggregate Into Min() ,子句會轉譯為的 Min調用。

另請參閱

適用於

.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

Min(IEnumerable<Nullable<Int64>>)

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

傳回可為 Null 之 Int64 值序列中的最小值。

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

參數

source
IEnumerable<Nullable<Int64>>

要判斷最小值之可為 Null 的 Int64 值序列。

傳回

對應至序列中最小值的值。

例外狀況

sourcenull

備註

方法Min(IEnumerable<Nullable<Int64>>)會使用的 IComparable<T> 實作Int64來比較值。

如果來源序列是空的,或只包含的值 null,則此函式會傳 null回 。

在 Visual Basic 查詢表達式語法中 Aggregate Into Min() ,子句會轉譯為的 Min調用。

另請參閱

適用於

.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

Min(IEnumerable<Nullable<Double>>)

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

傳回可為 Null 之 Double 值序列中的最小值。

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

參數

source
IEnumerable<Nullable<Double>>

要判斷最小值之可為 Null 的 Double 值序列。

傳回

對應至序列中最小值的值。

例外狀況

sourcenull

備註

方法Min(IEnumerable<Nullable<Double>>)會使用的 IComparable<T> 實作Double來比較值。

如果來源序列是空的,或只包含的值 null,則此函式會傳 null回 。

在 Visual Basic 查詢表達式語法中 Aggregate Into Min() ,子句會轉譯為的 Min調用。

另請參閱

適用於

.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

Min(IEnumerable<Decimal>)

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

傳回 Decimal 值序列中的最小值。

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

參數

source
IEnumerable<Decimal>

要判斷最小值的 Decimal 值序列。

傳回

序列中的最小值。

例外狀況

sourcenull

source 沒有包含任何項目。

備註

方法Min(IEnumerable<Decimal>)會使用的 IComparable<T> 實作Decimal來比較值。

在 Visual Basic 查詢表達式語法中 Aggregate Into Min() ,子句會轉譯為的 Min調用。

另請參閱

適用於

.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

Min(IEnumerable<Int64>)

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

傳回 Int64 值序列中的最小值。

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

參數

source
IEnumerable<Int64>

要判斷最小值的 Int64 值序列。

傳回

序列中的最小值。

例外狀況

sourcenull

source 沒有包含任何項目。

備註

方法 Min(IEnumerable<Int64>)Int64 使用的實作 IComparable<T> 來比較值。

在 Visual Basic 查詢表達式語法中, Aggregate Into Min() 子句會轉譯為的 Min調用。

另請參閱

適用於

.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

Min(IEnumerable<Int32>)

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

傳回 Int32 值序列中的最小值。

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

參數

source
IEnumerable<Int32>

要判斷最小值的 Int32 值序列。

傳回

序列中的最小值。

例外狀況

sourcenull

source 沒有包含任何項目。

備註

方法 Min(IEnumerable<Int32>)Int32 使用的實作 IComparable<T> 來比較值。

在 Visual Basic 查詢表達式語法中, Aggregate Into Min() 子句會轉譯為的 Min調用。

另請參閱

適用於

.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

Min(IEnumerable<Double>)

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

傳回 Double 值序列中的最小值。

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

參數

source
IEnumerable<Double>

要判斷最小值的 Double 值序列。

傳回

序列中的最小值。

例外狀況

sourcenull

source 沒有包含任何項目。

範例

下列程式代碼範例示範如何使用 Min(IEnumerable<Double>) 來判斷序列中的最小值。

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

double min = doubles.Min();

Console.WriteLine("The smallest number is {0}.", min);

/*
 This code produces the following output:

 The smallest number is -2E+103.
*/

備註

方法 Min(IEnumerable<Double>)Double 使用的實作 IComparable<T> 來比較值。

在 Visual Basic 查詢表達式語法中, Aggregate Into Min() 子句會轉譯為的 Min調用。

另請參閱

適用於

.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

Min(IEnumerable<Nullable<Decimal>>)

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

傳回可為 Null 之 Decimal 值序列中的最小值。

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

參數

source
IEnumerable<Nullable<Decimal>>

要判斷最小值之可為 Null 的 Decimal 值序列。

傳回

對應至序列中最小值的值。

例外狀況

sourcenull

備註

方法 Min(IEnumerable<Nullable<Decimal>>)Decimal 使用的實作 IComparable<T> 來比較值。

如果來源序列是空的,或只包含的值 null,則此函式會傳 null回 。

在 Visual Basic 查詢表達式語法中, Aggregate Into Min() 子句會轉譯為的 Min調用。

另請參閱

適用於

.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

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

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

在泛型序列的每個項目上叫用轉換函式,並傳回最小的結果值。

C#
public static TResult Min<TSource,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TResult> selector);
C#
public static TResult? Min<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

範例

下列程式代碼範例示範如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來判斷投影值序列中的最小值。

備註

此程式代碼範例會使用與本文所描述之特定多載不同的 方法多載。 若要將範例擴充至本文所描述的多載,請變更函式的 selector 主體。

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

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

    int min = pets.Min(pet => pet.Age);

    Console.WriteLine("The youngest animal is age {0}.", min);
}

/*
 This code produces the following output:

 The youngest animal is age 1.
*/

備註

如果類型 TResult 實作 IComparable<T>,這個方法會使用該實作來比較值。 否則,如果類型 TResult 實作 IComparable,則會使用該實作來比較值。

在 Visual Basic 查詢表達式語法中, Aggregate Into Min() 子句會轉譯為的 Min調用。

另請參閱

適用於

.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

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

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

在序列的每個項目上叫用轉換函式,並傳回最小的 Single 值。

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

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

要判斷最小值的值序列。

selector
Func<TSource,Single>

要套用至每個項目的轉換函式。

傳回

序列中的最小值。

例外狀況

sourceselectornull

source 沒有包含任何項目。

範例

下列程式代碼範例示範如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來判斷投影值序列中的最小值。

備註

此程式代碼範例會使用與本文所描述之特定多載不同的 方法多載。 若要將範例擴充至本文所描述的多載,請變更函式的 selector 主體。

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

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

    int min = pets.Min(pet => pet.Age);

    Console.WriteLine("The youngest animal is age {0}.", min);
}

/*
 This code produces the following output:

 The youngest animal is age 1.
*/

備註

方法 Min<TSource>(IEnumerable<TSource>, Func<TSource,Single>)Single 使用的實作 IComparable<T> 來比較值。

如果您提供函式 , selector將 的成員 source 投影到數值類型,特別是 Single,您可以將這個方法套用至任意值的序列。

在 Visual Basic 查詢表達式語法中, Aggregate Into Min() 子句會轉譯為的 Min調用。

另請參閱

適用於

.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

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

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

在序列的每個項目上叫用轉換函式,並傳回最小的可為 Null 之 Single 值。

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

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

要判斷最小值的值序列。

selector
Func<TSource,Nullable<Single>>

要套用至每個項目的轉換函式。

傳回

對應至序列中最小值的值。

例外狀況

sourceselectornull

範例

下列程式代碼範例示範如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來判斷投影值序列中的最小值。

備註

此程式代碼範例會使用與本文所描述之特定多載不同的 方法多載。 若要將範例擴充至本文所描述的多載,請變更函式的 selector 主體。

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

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

    int min = pets.Min(pet => pet.Age);

    Console.WriteLine("The youngest animal is age {0}.", min);
}

/*
 This code produces the following output:

 The youngest animal is age 1.
*/

備註

方法 Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)Single 使用的實作 IComparable<T> 來比較值。

如果您提供函式 , selector可將 的成員 source 投影成數值類型,特別是 Nullable<Single> C# 或 Nullable(Of Single) Visual Basic 中的函式,您可以將這個方法套用至任意值的序列。

在 Visual Basic 查詢表達式語法中, Aggregate Into Min() 子句會轉譯為的 Min調用。

另請參閱

適用於

.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

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

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

在序列的每個項目上叫用轉換函式,並傳回最小的可為 Null 之 Int64 值。

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

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

要判斷最小值的值序列。

selector
Func<TSource,Nullable<Int64>>

要套用至每個項目的轉換函式。

傳回

對應至序列中最小值的值。

例外狀況

sourceselectornull

範例

下列程式代碼範例示範如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來判斷投影值序列中的最小值。

備註

此程式代碼範例會使用與本文所描述之特定多載不同的 方法多載。 若要將範例擴充至本文所描述的多載,請變更函式的 selector 主體。

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

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

    int min = pets.Min(pet => pet.Age);

    Console.WriteLine("The youngest animal is age {0}.", min);
}

/*
 This code produces the following output:

 The youngest animal is age 1.
*/

備註

方法 Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)Int64 使用的實作 IComparable<T> 來比較值。

如果您提供函式 , selector可將 的成員 source 投影成數值類型,特別是 Nullable<Int64> C# 或 Nullable(Of Int64) Visual Basic 中的函式,您可以將這個方法套用至任意值的序列。

在 Visual Basic 查詢表達式語法中, Aggregate Into Min() 子句會轉譯為的 Min調用。

另請參閱

適用於

.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

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

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

在序列的每個項目上叫用轉換函式,並傳回最小的可為 Null 之 Double 值。

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

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

要判斷最小值的值序列。

selector
Func<TSource,Nullable<Double>>

要套用至每個項目的轉換函式。

傳回

對應至序列中最小值的值。

例外狀況

sourceselectornull

範例

下列程式代碼範例示範如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來判斷投影值序列中的最小值。

備註

此程式代碼範例會使用與本文所描述之特定多載不同的 方法多載。 若要將範例擴充至本文所描述的多載,請變更函式的 selector 主體。

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

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

    int min = pets.Min(pet => pet.Age);

    Console.WriteLine("The youngest animal is age {0}.", min);
}

/*
 This code produces the following output:

 The youngest animal is age 1.
*/

備註

方法 Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)Double 使用的實作 IComparable<T> 來比較值。

如果您提供函式 , selector可將 的成員 source 投影成數值類型,特別是 Nullable<Double> C# 或 Nullable(Of Double) Visual Basic 中的函式,您可以將這個方法套用至任意值的序列。

在 Visual Basic 查詢表達式語法中, Aggregate Into Min() 子句會轉譯為的 Min調用。

另請參閱

適用於

.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

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

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

在序列的每個項目上叫用轉換函式,並傳回最小的可為 Null 之 Decimal 值。

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

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

要判斷最小值的值序列。

selector
Func<TSource,Nullable<Decimal>>

要套用至每個項目的轉換函式。

傳回

對應至序列中最小值的值。

例外狀況

sourceselectornull

範例

下列程式代碼範例示範如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來判斷投影值序列中的最小值。

備註

此程式代碼範例會使用與本文所描述之特定多載不同的 方法多載。 若要將範例擴充至本文所描述的多載,請變更函式的 selector 主體。

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

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

    int min = pets.Min(pet => pet.Age);

    Console.WriteLine("The youngest animal is age {0}.", min);
}

/*
 This code produces the following output:

 The youngest animal is age 1.
*/

備註

方法 Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)Decimal 使用的實作 IComparable<T> 來比較值。

如果您提供函式 , selector可將 的成員 source 投影成數值類型,特別是 Nullable<Decimal> C# 或 Nullable(Of Decimal) Visual Basic 中的函式,您可以將這個方法套用至任意值的序列。

在 Visual Basic 查詢表達式語法中, Aggregate Into Min() 子句會轉譯為的 Min調用。

另請參閱

適用於

.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

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

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

在序列的每個項目上叫用轉換函式,並傳回最小的可為 Null 之 Int32 值。

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

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

要判斷最小值的值序列。

selector
Func<TSource,Nullable<Int32>>

要套用至每個項目的轉換函式。

傳回

對應至序列中最小值的值。

例外狀況

sourceselectornull

範例

下列程式代碼範例示範如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來判斷投影值序列中的最小值。

備註

此程式代碼範例會使用與本文所描述之特定多載不同的 方法多載。 若要將範例擴充至本文所描述的多載,請變更函式的 selector 主體。

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

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

    int min = pets.Min(pet => pet.Age);

    Console.WriteLine("The youngest animal is age {0}.", min);
}

/*
 This code produces the following output:

 The youngest animal is age 1.
*/

備註

方法 Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)Int32 使用的實作 IComparable<T> 來比較值。

如果您提供函式 , selector可將來源成員投影到數值類型,特別是 Nullable<Int32> C# 或 Nullable(Of Int32) Visual Basic 中的函式,您可以將這個方法套用至任意值的序列。

在 Visual Basic 查詢表達式語法中, Aggregate Into Min() 子句會轉譯為的 Min調用。

另請參閱

適用於

.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

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

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

在序列的每個項目上叫用轉換函式,並傳回最小的 Int32 值。

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

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

要判斷最小值的值序列。

selector
Func<TSource,Int32>

要套用至每個項目的轉換函式。

傳回

序列中的最小值。

例外狀況

sourceselectornull

source 沒有包含任何項目。

範例

下列程式代碼範例示範如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來判斷投影值序列中的最小值。

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

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

    int min = pets.Min(pet => pet.Age);

    Console.WriteLine("The youngest animal is age {0}.", min);
}

/*
 This code produces the following output:

 The youngest animal is age 1.
*/

備註

方法 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)Int32 使用的實作 IComparable<T> 來比較值。

如果您提供函式 , selector將 的成員 source 投影到數值類型,特別是 Int32,您可以將這個方法套用至任意值的序列。

在 Visual Basic 查詢表達式語法中, Aggregate Into Min() 子句會轉譯為的 Min調用。

另請參閱

適用於

.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

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

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

在序列的每個項目上叫用轉換函式,並傳回最小的 Double 值。

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

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

要判斷最小值的值序列。

selector
Func<TSource,Double>

要套用至每個項目的轉換函式。

傳回

序列中的最小值。

例外狀況

sourceselectornull

source 沒有包含任何項目。

範例

下列程式代碼範例示範如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來判斷投影值序列中的最小值。

備註

此程式代碼範例會使用與本文所描述之特定多載不同的 方法多載。 若要將範例擴充至本文所描述的多載,請變更函式的 selector 主體。

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

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

    int min = pets.Min(pet => pet.Age);

    Console.WriteLine("The youngest animal is age {0}.", min);
}

/*
 This code produces the following output:

 The youngest animal is age 1.
*/

備註

方法 Min<TSource>(IEnumerable<TSource>, Func<TSource,Double>)Double 使用的實作 IComparable<T> 來比較值。

如果您提供函式 , selector將 的成員 source 投影到數值類型,特別是 Double,您可以將這個方法套用至任意值的序列。

在 Visual Basic 查詢表達式語法中, Aggregate Into Min() 子句會轉譯為的 Min調用。

另請參閱

適用於

.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

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

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

在序列的每個項目上叫用轉換函式,並傳回最小的 Decimal 值。

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

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

要判斷最小值的值序列。

selector
Func<TSource,Decimal>

要套用至每個項目的轉換函式。

傳回

序列中的最小值。

例外狀況

sourceselectornull

source 沒有包含任何項目。

範例

下列程式代碼範例示範如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來判斷投影值序列中的最小值。

備註

此程式代碼範例會使用與本文所描述之特定多載不同的 方法多載。 若要將範例擴充至本文所描述的多載,請變更函式的 selector 主體。

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

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

    int min = pets.Min(pet => pet.Age);

    Console.WriteLine("The youngest animal is age {0}.", min);
}

/*
 This code produces the following output:

 The youngest animal is age 1.
*/

備註

方法 Min<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)Decimal 使用的實作 IComparable<T> 來比較值。

如果您提供函式 , selector將 的成員 source 投影到數值類型,特別是 Decimal,您可以將這個方法套用至任意值的序列。

在 Visual Basic 查詢表達式語法中, Aggregate Into Min() 子句會轉譯為的 Min調用。

另請參閱

適用於

.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

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

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

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

C#
public static TSource? Min<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> 介面。

備註

如果類型 TSource 實作 IComparable<T>,則 Max<TSource>(IEnumerable<TSource>) 方法會使用該實作來比較值。 否則,如果類型 TSource 實作 IComparable,則會使用該實作來比較值。

如果 TSource 參考型別,而且來源序列是空的,或只包含的值 null,則這個方法會傳 null回 。

在 Visual Basic 查詢表達式語法中, Aggregate Into Min() 子句會轉譯為的 Min調用。

適用於

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

Min<TSource>(IEnumerable<TSource>)

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

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

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

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

要判斷最小值的值序列。

傳回

TSource

序列中的最小值。

例外狀況

sourcenull

source 中沒有任何物件實作 IComparableIComparable<T> 介面。

範例

下列程式代碼範例示範如何使用 Min<TSource>(IEnumerable<TSource>) 來判斷物件序列 IComparable<T> 中的最小值。

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

    /// <summary>
    /// Compares this Pet's age to another Pet's age.
    /// </summary>
    /// <param name="other">The Pet to compare this Pet to.</param>
    /// <returns>-1 if this Pet's age is smaller,
    /// 0 if the Pets' ages are equal, or
    /// 1 if this Pet's age is greater.</returns>
    int IComparable<Pet>.CompareTo(Pet other)
    {
        if (other.Age > this.Age)
            return -1;
        else if (other.Age == this.Age)
            return 0;
        else
            return 1;
    }
}

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

    Pet min = pets.Min();

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

/*
 This code produces the following output:

 The 'minimum' animal is Whiskers.
*/

備註

如果類型 TSource 實作 IComparable<T>,這個方法會使用該實作來比較值。 否則,如果類型 TSource 實作 IComparable,則會使用該實作來比較值。

如果 TSource 參考型別,而且來源序列是空的,或只包含的值 null,則此函式會傳 null回 。

在 Visual Basic 查詢表達式語法中, Aggregate Into Min() 子句會轉譯為的 Min調用。

另請參閱

適用於

.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

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

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

在序列的每個項目上叫用轉換函式,並傳回最小的 Int64 值。

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

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

要判斷最小值的值序列。

selector
Func<TSource,Int64>

要套用至每個項目的轉換函式。

傳回

序列中的最小值。

例外狀況

sourceselectornull

source 沒有包含任何項目。

範例

下列程式代碼範例示範如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 來判斷投影值序列中的最小值。

備註

此程式代碼範例會使用與本文所描述之特定多載不同的 方法多載。 若要將範例擴充至本文所描述的多載,請變更函式的 selector 主體。

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

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

    int min = pets.Min(pet => pet.Age);

    Console.WriteLine("The youngest animal is age {0}.", min);
}

/*
 This code produces the following output:

 The youngest animal is age 1.
*/

備註

方法 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)Int64 使用的實作 IComparable<T> 來比較值。

如果您提供函式 , selector將 的成員 source 投影到數值類型,特別是 Int64,您可以將這個方法套用至任意值的序列。

在 Visual Basic 查詢表達式語法中, Aggregate Into Min() 子句會轉譯為的 Min調用。

另請參閱

適用於

.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