Enumerable.Min Method

Definition

Returns the minimum value in a sequence of values.

Overloads

Min(IEnumerable<Nullable<Int64>>)

Returns the minimum value in a sequence of nullable Int64 values.

Min(IEnumerable<Nullable<Int32>>)

Returns the minimum value in a sequence of nullable Int32 values.

Min(IEnumerable<Single>)

Returns the minimum value in a sequence of Single values.

Min(IEnumerable<Nullable<Single>>)

Returns the minimum value in a sequence of nullable Single values.

Min(IEnumerable<Nullable<Double>>)

Returns the minimum value in a sequence of nullable Double values.

Min(IEnumerable<Double>)

Returns the minimum value in a sequence of Double values.

Min(IEnumerable<Int64>)

Returns the minimum value in a sequence of Int64 values.

Min(IEnumerable<Int32>)

Returns the minimum value in a sequence of Int32 values.

Min(IEnumerable<Nullable<Decimal>>)

Returns the minimum value in a sequence of nullable Decimal values.

Min(IEnumerable<Decimal>)

Returns the minimum value in a sequence of Decimal values.

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

Invokes a transform function on each element of a generic sequence and returns the minimum resulting value.

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

Invokes a transform function on each element of a sequence and returns the minimum Single value.

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

Invokes a transform function on each element of a sequence and returns the minimum nullable Single value.

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

Invokes a transform function on each element of a sequence and returns the minimum nullable Int64 value.

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

Invokes a transform function on each element of a sequence and returns the minimum nullable Int32 value.

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

Invokes a transform function on each element of a sequence and returns the minimum nullable Double value.

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

Invokes a transform function on each element of a sequence and returns the minimum Int64 value.

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

Invokes a transform function on each element of a sequence and returns the minimum Int32 value.

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

Invokes a transform function on each element of a sequence and returns the minimum Double value.

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

Invokes a transform function on each element of a sequence and returns the minimum Decimal value.

Min<TSource>(IEnumerable<TSource>)

Returns the minimum value in a generic sequence.

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

Invokes a transform function on each element of a sequence and returns the minimum nullable Decimal value.

Min(IEnumerable<Nullable<Int64>>)

Returns the minimum value in a sequence of nullable Int64 values.

public static Nullable<long> Min (this System.Collections.Generic.IEnumerable<Nullable<long>> source);
Parameters
source
IEnumerable<Nullable<Int64>>

A sequence of nullable Int64 values to determine the minimum value of.

Returns

A value of type Nullable<Int64> in C# or Nullable(Of Int64) in Visual Basic that corresponds to the minimum value in the sequence.

Exceptions

source is null.

Examples

The following code example demonstrates how to use Min(IEnumerable<Nullable<Int32>>) to determine the minimum value in a sequence.

Note

This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of the appropriate numerical type.

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.
*/

Dim grades() As Nullable(Of Integer) = {78, 92, Nothing, 99, 37, 81}
Dim min As Nullable(Of Integer) = grades.Min()

' Display the output.
MsgBox("The lowest grade is " & min)

' This code produces the following output:
'
' The lowest grade is 37

Remarks

The Min(IEnumerable<Nullable<Int64>>) method uses the Int64 implementation of IComparable<T> to compare values.

If the source sequence is empty or contains only values that are null, this function returns null.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

Min(IEnumerable<Nullable<Int32>>)

Returns the minimum value in a sequence of nullable Int32 values.

public static Nullable<int> Min (this System.Collections.Generic.IEnumerable<Nullable<int>> source);
Parameters
source
IEnumerable<Nullable<Int32>>

A sequence of nullable Int32 values to determine the minimum value of.

Returns

A value of type Nullable<Int32> in C# or Nullable(Of Int32) in Visual Basic that corresponds to the minimum value in the sequence.

Exceptions

source is null.

Examples

The following code example demonstrates how to use Min(IEnumerable<Nullable<Int32>>) to determine the minimum value in a sequence.

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.
*/

Dim grades() As Nullable(Of Integer) = {78, 92, Nothing, 99, 37, 81}
Dim min As Nullable(Of Integer) = grades.Min()

' Display the output.
MsgBox("The lowest grade is " & min)

' This code produces the following output:
'
' The lowest grade is 37

Remarks

The Min(IEnumerable<Nullable<Int32>>) method uses the Int32 implementation of IComparable<T> to compare values.

If the source sequence is empty or contains only values that are null, this function returns null.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

Min(IEnumerable<Single>)

Returns the minimum value in a sequence of Single values.

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

A sequence of Single values to determine the minimum value of.

Returns

The minimum value in the sequence.

Exceptions

source is null.

source contains no elements.

Examples

The following code example demonstrates how to use Min(IEnumerable<Double>) to determine the minimum value in a sequence.

Note

This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of the appropriate numerical type.

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.
*/

' Create an array of double values.
Dim doubles() As Double = {1.5E+104, 9.0E+103, -2.0E+103}

' Determine the smallest number in the array.
Dim min As Double = doubles.Min()

' Display the result.
MsgBox("The smallest number is " & min)

' This code produces the following output:
'
' The smallest number is -2E+103

Remarks

The Min(IEnumerable<Single>) method uses the Single implementation of IComparable<T> to compare values.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

Min(IEnumerable<Nullable<Single>>)

Returns the minimum value in a sequence of nullable Single values.

public static Nullable<float> Min (this System.Collections.Generic.IEnumerable<Nullable<float>> source);
Parameters
source
IEnumerable<Nullable<Single>>

A sequence of nullable Single values to determine the minimum value of.

Returns

A value of type Nullable<Single> in C# or Nullable(Of Single) in Visual Basic that corresponds to the minimum value in the sequence.

Exceptions

source is null.

Examples

The following code example demonstrates how to use Min(IEnumerable<Nullable<Int32>>) to determine the minimum value in a sequence.

Note

This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of the appropriate numerical type.

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.
*/

Dim grades() As Nullable(Of Integer) = {78, 92, Nothing, 99, 37, 81}
Dim min As Nullable(Of Integer) = grades.Min()

' Display the output.
MsgBox("The lowest grade is " & min)

' This code produces the following output:
'
' The lowest grade is 37

Remarks

The Min(IEnumerable<Nullable<Single>>) method uses the Single implementation of IComparable<T> to compare values.

If the source sequence is empty or contains only values that are null, this function returns null.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

Min(IEnumerable<Nullable<Double>>)

Returns the minimum value in a sequence of nullable Double values.

public static Nullable<double> Min (this System.Collections.Generic.IEnumerable<Nullable<double>> source);
Parameters
source
IEnumerable<Nullable<Double>>

A sequence of nullable Double values to determine the minimum value of.

Returns

A value of type Nullable<Double> in C# or Nullable(Of Double) in Visual Basic that corresponds to the minimum value in the sequence.

Exceptions

source is null.

Examples

The following code example demonstrates how to use Min(IEnumerable<Nullable<Int32>>) to determine the minimum value in a sequence.

Note

This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of the appropriate numerical type.

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.
*/

Dim grades() As Nullable(Of Integer) = {78, 92, Nothing, 99, 37, 81}
Dim min As Nullable(Of Integer) = grades.Min()

' Display the output.
MsgBox("The lowest grade is " & min)

' This code produces the following output:
'
' The lowest grade is 37

Remarks

The Min(IEnumerable<Nullable<Double>>) method uses the Double implementation of IComparable<T> to compare values.

If the source sequence is empty or contains only values that are null, this function returns null.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

Min(IEnumerable<Double>)

Returns the minimum value in a sequence of Double values.

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

A sequence of Double values to determine the minimum value of.

Returns

The minimum value in the sequence.

Exceptions

source is null.

source contains no elements.

Examples

The following code example demonstrates how to use Min(IEnumerable<Double>) to determine the minimum value in a sequence.

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.
*/

' Create an array of double values.
Dim doubles() As Double = {1.5E+104, 9.0E+103, -2.0E+103}

' Determine the smallest number in the array.
Dim min As Double = doubles.Min()

' Display the result.
MsgBox("The smallest number is " & min)

' This code produces the following output:
'
' The smallest number is -2E+103

Remarks

The Min(IEnumerable<Double>) method uses the Double implementation of IComparable<T> to compare values.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

Min(IEnumerable<Int64>)

Returns the minimum value in a sequence of Int64 values.

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

A sequence of Int64 values to determine the minimum value of.

Returns

The minimum value in the sequence.

Exceptions

source is null.

source contains no elements.

Examples

The following code example demonstrates how to use Min(IEnumerable<Double>) to determine the minimum value in a sequence.

Note

This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of the appropriate numerical type.

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.
*/

' Create an array of double values.
Dim doubles() As Double = {1.5E+104, 9.0E+103, -2.0E+103}

' Determine the smallest number in the array.
Dim min As Double = doubles.Min()

' Display the result.
MsgBox("The smallest number is " & min)

' This code produces the following output:
'
' The smallest number is -2E+103

Remarks

The Min(IEnumerable<Int64>) method uses the Int64 implementation of IComparable<T> to compare values.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

Min(IEnumerable<Int32>)

Returns the minimum value in a sequence of Int32 values.

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

A sequence of Int32 values to determine the minimum value of.

Returns

The minimum value in the sequence.

Exceptions

source is null.

source contains no elements.

Examples

The following code example demonstrates how to use Min(IEnumerable<Double>) to determine the minimum value in a sequence.

Note

This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of the appropriate numerical type.

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.
*/

' Create an array of double values.
Dim doubles() As Double = {1.5E+104, 9.0E+103, -2.0E+103}

' Determine the smallest number in the array.
Dim min As Double = doubles.Min()

' Display the result.
MsgBox("The smallest number is " & min)

' This code produces the following output:
'
' The smallest number is -2E+103

Remarks

The Min(IEnumerable<Int32>) method uses the Int32 implementation of IComparable<T> to compare values.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

Min(IEnumerable<Nullable<Decimal>>)

Returns the minimum value in a sequence of nullable Decimal values.

public static Nullable<decimal> Min (this System.Collections.Generic.IEnumerable<Nullable<decimal>> source);
Parameters
source
IEnumerable<Nullable<Decimal>>

A sequence of nullable Decimal values to determine the minimum value of.

Returns

A value of type Nullable<Decimal> in C# or Nullable(Of Decimal) in Visual Basic that corresponds to the minimum value in the sequence.

Exceptions

source is null.

Examples

The following code example demonstrates how to use Min(IEnumerable<Nullable<Int32>>) to determine the minimum value in a sequence.

Note

This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of the appropriate numerical type.

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.
*/

Dim grades() As Nullable(Of Integer) = {78, 92, Nothing, 99, 37, 81}
Dim min As Nullable(Of Integer) = grades.Min()

' Display the output.
MsgBox("The lowest grade is " & min)

' This code produces the following output:
'
' The lowest grade is 37

Remarks

The Min(IEnumerable<Nullable<Decimal>>) method uses the Decimal implementation of IComparable<T> to compare values.

If the source sequence is empty or contains only values that are null, this function returns null.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

Min(IEnumerable<Decimal>)

Returns the minimum value in a sequence of Decimal values.

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

A sequence of Decimal values to determine the minimum value of.

Returns

The minimum value in the sequence.

Exceptions

source is null.

source contains no elements.

Examples

The following code example demonstrates how to use Min(IEnumerable<Double>) to determine the minimum value in a sequence.

Note

This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of the appropriate numerical type.

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.
*/

' Create an array of double values.
Dim doubles() As Double = {1.5E+104, 9.0E+103, -2.0E+103}

' Determine the smallest number in the array.
Dim min As Double = doubles.Min()

' Display the result.
MsgBox("The smallest number is " & min)

' This code produces the following output:
'
' The smallest number is -2E+103

Remarks

The Min(IEnumerable<Decimal>) method uses the Decimal implementation of IComparable<T> to compare values.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

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

Invokes a transform function on each element of a generic sequence and returns the minimum resulting value.

public static TResult Min<TSource,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TResult> selector);
Type Parameters
TSource

The type of the elements of source.

TResult

The type of the value returned by selector.

Parameters
source
IEnumerable<TSource>

A sequence of values to determine the minimum value of.

selector
Func<TSource,TResult>

A transform function to apply to each element.

Returns

The minimum value in the sequence.

Exceptions

source or selector is null.

Examples

The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the minimum value in a sequence of projected values.

Note

This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, change the body of the selector function.

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.  
*/

Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Sub MinEx4()
    ' 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 youngest pet by passing a 
    ' lambda expression to the Min() method.
    Dim min As Integer = pets.Min(Function(pet) pet.Age)

    ' Display the result.
    MsgBox("The youngest pet is age " & min)
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

Remarks

If type TResult implements IComparable<T>, this method uses that implementation to compare values. Otherwise, if type TResult implements IComparable, that implementation is used to compare values.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

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

Invokes a transform function on each element of a sequence and returns the minimum Single value.

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

The type of the elements of source.

Parameters
source
IEnumerable<TSource>

A sequence of values to determine the minimum value of.

selector
Func<TSource,Single>

A transform function to apply to each element.

Returns

The minimum value in the sequence.

Exceptions

source or selector is null.

source contains no elements.

Examples

The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the minimum value in a sequence of projected values.

Note

This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, change the body of the selector function.

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.  
*/

Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Sub MinEx4()
    ' 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 youngest pet by passing a 
    ' lambda expression to the Min() method.
    Dim min As Integer = pets.Min(Function(pet) pet.Age)

    ' Display the result.
    MsgBox("The youngest pet is age " & min)
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

Remarks

The Min<TSource>(IEnumerable<TSource>, Func<TSource,Single>) method uses the Single implementation of IComparable<T> to compare values.

You can apply this method to a sequence of arbitrary values if you provide a function, selector, that projects the members of source into a numeric type, specifically Single.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

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

Invokes a transform function on each element of a sequence and returns the minimum nullable Single value.

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

The type of the elements of source.

Parameters
source
IEnumerable<TSource>

A sequence of values to determine the minimum value of.

selector
Func<TSource,Nullable<Single>>

A transform function to apply to each element.

Returns

The value of type Nullable<Single> in C# or Nullable(Of Single) in Visual Basic that corresponds to the minimum value in the sequence.

Exceptions

source or selector is null.

Examples

The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the minimum value in a sequence of projected values.

Note

This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, change the body of the selector function.

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.  
*/

Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Sub MinEx4()
    ' 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 youngest pet by passing a 
    ' lambda expression to the Min() method.
    Dim min As Integer = pets.Min(Function(pet) pet.Age)

    ' Display the result.
    MsgBox("The youngest pet is age " & min)
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

Remarks

The Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>) method uses the Single implementation of IComparable<T> to compare values.

You can apply this method to a sequence of arbitrary values if you provide a function, selector, that projects the members of source into a numeric type, specifically Nullable<Single> in C# or Nullable(Of Single) in Visual Basic.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

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

Invokes a transform function on each element of a sequence and returns the minimum nullable Int64 value.

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

The type of the elements of source.

Parameters
source
IEnumerable<TSource>

A sequence of values to determine the minimum value of.

selector
Func<TSource,Nullable<Int64>>

A transform function to apply to each element.

Returns

The value of type Nullable<Int64> in C# or Nullable(Of Int64) in Visual Basic that corresponds to the minimum value in the sequence.

Exceptions

source or selector is null.

Examples

The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the minimum value in a sequence of projected values.

Note

This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, change the body of the selector function.

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.  
*/

Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Sub MinEx4()
    ' 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 youngest pet by passing a 
    ' lambda expression to the Min() method.
    Dim min As Integer = pets.Min(Function(pet) pet.Age)

    ' Display the result.
    MsgBox("The youngest pet is age " & min)
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

Remarks

The Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>) method uses the Int64 implementation of IComparable<T> to compare values.

You can apply this method to a sequence of arbitrary values if you provide a function, selector, that projects the members of source into a numeric type, specifically Nullable<Int64> in C# or Nullable(Of Int64) in Visual Basic.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

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

Invokes a transform function on each element of a sequence and returns the minimum nullable Int32 value.

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

The type of the elements of source.

Parameters
source
IEnumerable<TSource>

A sequence of values to determine the minimum value of.

selector
Func<TSource,Nullable<Int32>>

A transform function to apply to each element.

Returns

The value of type Nullable<Int32> in C# or Nullable(Of Int32) in Visual Basic that corresponds to the minimum value in the sequence.

Exceptions

source or selector is null.

Examples

The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the minimum value in a sequence of projected values.

Note

This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, change the body of the selector function.

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.  
*/

Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Sub MinEx4()
    ' 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 youngest pet by passing a 
    ' lambda expression to the Min() method.
    Dim min As Integer = pets.Min(Function(pet) pet.Age)

    ' Display the result.
    MsgBox("The youngest pet is age " & min)
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

Remarks

The Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>) method uses the Int32 implementation of IComparable<T> to compare values.

You can apply this method to a sequence of arbitrary values if you provide a function, selector, that projects the members of source into a numeric type, specifically Nullable<Int32> in C# or Nullable(Of Int32) in Visual Basic.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

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

Invokes a transform function on each element of a sequence and returns the minimum nullable Double value.

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

The type of the elements of source.

Parameters
source
IEnumerable<TSource>

A sequence of values to determine the minimum value of.

selector
Func<TSource,Nullable<Double>>

A transform function to apply to each element.

Returns

The value of type Nullable<Double> in C# or Nullable(Of Double) in Visual Basic that corresponds to the minimum value in the sequence.

Exceptions

source or selector is null.

Examples

The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the minimum value in a sequence of projected values.

Note

This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, change the body of the selector function.

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.  
*/

Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Sub MinEx4()
    ' 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 youngest pet by passing a 
    ' lambda expression to the Min() method.
    Dim min As Integer = pets.Min(Function(pet) pet.Age)

    ' Display the result.
    MsgBox("The youngest pet is age " & min)
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

Remarks

The Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>) method uses the Double implementation of IComparable<T> to compare values.

You can apply this method to a sequence of arbitrary values if you provide a function, selector, that projects the members of source into a numeric type, specifically Nullable<Double> in C# or Nullable(Of Double) in Visual Basic.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

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

Invokes a transform function on each element of a sequence and returns the minimum Int64 value.

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

The type of the elements of source.

Parameters
source
IEnumerable<TSource>

A sequence of values to determine the minimum value of.

selector
Func<TSource,Int64>

A transform function to apply to each element.

Returns

The minimum value in the sequence.

Exceptions

source or selector is null.

source contains no elements.

Examples

The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the minimum value in a sequence of projected values.

Note

This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, change the body of the selector function.

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.  
*/

Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Sub MinEx4()
    ' 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 youngest pet by passing a 
    ' lambda expression to the Min() method.
    Dim min As Integer = pets.Min(Function(pet) pet.Age)

    ' Display the result.
    MsgBox("The youngest pet is age " & min)
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

Remarks

The Min<TSource>(IEnumerable<TSource>, Func<TSource,Int64>) method uses the Int64 implementation of IComparable<T> to compare values.

You can apply this method to a sequence of arbitrary values if you provide a function, selector, that projects the members of source into a numeric type, specifically Int64.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

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

Invokes a transform function on each element of a sequence and returns the minimum Int32 value.

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

The type of the elements of source.

Parameters
source
IEnumerable<TSource>

A sequence of values to determine the minimum value of.

selector
Func<TSource,Int32>

A transform function to apply to each element.

Returns

The minimum value in the sequence.

Exceptions

source or selector is null.

source contains no elements.

Examples

The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the minimum value in a sequence of projected values.

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.  
*/

Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Sub MinEx4()
    ' 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 youngest pet by passing a 
    ' lambda expression to the Min() method.
    Dim min As Integer = pets.Min(Function(pet) pet.Age)

    ' Display the result.
    MsgBox("The youngest pet is age " & min)
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

Remarks

The Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) method uses the Int32 implementation of IComparable<T> to compare values.

You can apply this method to a sequence of arbitrary values if you provide a function, selector, that projects the members of source into a numeric type, specifically Int32.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

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

Invokes a transform function on each element of a sequence and returns the minimum Double value.

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

The type of the elements of source.

Parameters
source
IEnumerable<TSource>

A sequence of values to determine the minimum value of.

selector
Func<TSource,Double>

A transform function to apply to each element.

Returns

The minimum value in the sequence.

Exceptions

source or selector is null.

source contains no elements.

Examples

The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the minimum value in a sequence of projected values.

Note

This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, change the body of the selector function.

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.  
*/

Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Sub MinEx4()
    ' 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 youngest pet by passing a 
    ' lambda expression to the Min() method.
    Dim min As Integer = pets.Min(Function(pet) pet.Age)

    ' Display the result.
    MsgBox("The youngest pet is age " & min)
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

Remarks

The Min<TSource>(IEnumerable<TSource>, Func<TSource,Double>) method uses the Double implementation of IComparable<T> to compare values.

You can apply this method to a sequence of arbitrary values if you provide a function, selector, that projects the members of source into a numeric type, specifically Double.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

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

Invokes a transform function on each element of a sequence and returns the minimum Decimal value.

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

The type of the elements of source.

Parameters
source
IEnumerable<TSource>

A sequence of values to determine the minimum value of.

selector
Func<TSource,Decimal>

A transform function to apply to each element.

Returns

The minimum value in the sequence.

Exceptions

source or selector is null.

source contains no elements.

Examples

The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the minimum value in a sequence of projected values.

Note

This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, change the body of the selector function.

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.  
*/

Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Sub MinEx4()
    ' 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 youngest pet by passing a 
    ' lambda expression to the Min() method.
    Dim min As Integer = pets.Min(Function(pet) pet.Age)

    ' Display the result.
    MsgBox("The youngest pet is age " & min)
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

Remarks

The Min<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>) method uses the Decimal implementation of IComparable<T> to compare values.

You can apply this method to a sequence of arbitrary values if you provide a function, selector, that projects the members of source into a numeric type, specifically Decimal.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

Min<TSource>(IEnumerable<TSource>)

Returns the minimum value in a generic sequence.

public static TSource Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
Type Parameters
TSource

The type of the elements of source.

Parameters
source
IEnumerable<TSource>

A sequence of values to determine the minimum value of.

Returns

The minimum value in the sequence.

Exceptions

source is null.

Examples

The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>) to determine the minimum value in a sequence of IComparable<T> objects.

/// <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.
*/

' 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 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>
    Function CompareTo(ByVal other As Pet) As Integer _
    Implements IComparable(Of Pet).CompareTo

        If (other.Age > Me.Age) Then
            Return -1
        ElseIf (other.Age = Me.Age) Then
            Return 0
        Else
            Return 1
        End If
    End Function
End Class

Sub MinEx3()
    ' 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 "minimum" pet in the array,
    ' according to the custom CompareTo() implementation.
    Dim min As Pet = pets.Min()

    ' Display the result.
    MsgBox("The 'minimum' pet is " & min.Name)
End Sub

' This code produces the following output:
'
' The 'minimum' pet is Whiskers

Remarks

If type TSource implements IComparable<T>, this method uses that implementation to compare values. Otherwise, if type TSource implements IComparable, that implementation is used to compare values.

If TSource is a reference type and the source sequence is empty or contains only values that are null, this function returns null.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

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

Invokes a transform function on each element of a sequence and returns the minimum nullable Decimal value.

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

The type of the elements of source.

Parameters
source
IEnumerable<TSource>

A sequence of values to determine the minimum value of.

selector
Func<TSource,Nullable<Decimal>>

A transform function to apply to each element.

Returns

The value of type Nullable<Decimal> in C# or Nullable(Of Decimal) in Visual Basic that corresponds to the minimum value in the sequence.

Exceptions

source or selector is null.

Examples

The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the minimum value in a sequence of projected values.

Note

This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, change the body of the selector function.

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.  
*/

Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Sub MinEx4()
    ' 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 youngest pet by passing a 
    ' lambda expression to the Min() method.
    Dim min As Integer = pets.Min(Function(pet) pet.Age)

    ' Display the result.
    MsgBox("The youngest pet is age " & min)
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

Remarks

The Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>) method uses the Decimal implementation of IComparable<T> to compare values.

You can apply this method to a sequence of arbitrary values if you provide a function, selector, that projects the members of source into a numeric type, specifically Nullable<Decimal> in C# or Nullable(Of Decimal) in Visual Basic.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

Applies to