Min Element from IEnumerable - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Min Element from IEnumerable

Demo Code


using System.Linq;
using System.Collections.Generic;
using System;/*ww  w .java  2 s .c o  m*/

public class Main{
        public static T MinElement<T>(this IEnumerable<T> enumerable, Func<T, int> selector)
    {
      var minRank = int.MaxValue;
      var minElement = default(T);

      foreach (var element in enumerable)
      {
        var rank = selector(element);

        if (rank < minRank)
        {
          minRank = rank;
          minElement = element;
        }
      }

      return minElement;
    }
}

Related Tutorials