CSharp - LINQ Min


The Min operator returns the minimum value of an input sequence.


There are four prototypes we cover. The First Min Prototype

public static Numeric Min(
  this IEnumerable<Numeric> source);

The Numeric type must be one of int, long, double, or decimal or one of their nullable equivalents, int?, long?, double?, or decimal?.

The first prototype of the Min operator returns the element with the minimum numeric value in the source input sequence.

If the element type implements the IComparable<T> interface, that interface will be used to compare the elements.

If the elements do not implement the IComparable<T> interface, the nongeneric IComparable interface will be used.

An empty sequence, or one that contains only null values, will return the value of null.

The second prototype of the Min operator is for non-Numeric types.

public static T Min<T>(this IEnumerable<T> source);

The third prototype is for Numeric types with a selector method..

public static Numeric Min<T>(
    this IEnumerable<T> source,
     Func<T, Numeric> selector);

The fourth prototype is for non-Numeric types with a selector method.

public static S Min<T, S>(
        this IEnumerable<T> source,
        Func<T, S> selector);


  • ArgumentNullException is thrown if any argument is null.
  • InvalidOperationException is thrown if the source sequence is empty for the Numeric versions of the prototypes if the type T is non-nullable, such as int, long, double, or decimal. If the types are nullable, that is, int?, long?, double?, or decimal?, a null is returned from the operator instead.


using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Program/*from  w  w  w.j a  v a2s.  c  o  m*/
    static void Main(string[] args)
       int[] myInts = new int[] { 974, 2, 7, 1374, 27, 54 };
       int minInt = myInts.Min();


Related Topics