C# Enumerable Min(IEnumerable, Func)

Description

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

Syntax


public static decimal Min<TSource>(
  this IEnumerable<TSource> source,
  Func<TSource, decimal> selector
)

Parameters

  • TSource - The type of the elements of source.
  • source - A sequence of values to determine the minimum value of.
  • selector - A transform function to apply to each element.

Returns

returns The minimum value in the sequence.

Example

The following code example demonstrates how to use Min to determine the minimum value in a sequence of projected values.


//from   ww  w . jav  a  2s.  c om
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  

    Pet[] pets = { new Pet { Name="a", Age=8 },
                   new Pet { Name="b", Age=4 },
                   new Pet { Name="c", Age=1 } };

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

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

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

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Linq »




Enumerable