Mega Code Archive

 
Categories / C# Book / 04 LINQ
 

0406 Max

Input: IEnumerable<TSource> Optional lambda expression: TSource => TResult Max returns the smallest or largest element from a sequence: using System; using System.Collections; using System.Collections.Generic; using System.Linq; class Program { static void Main() { int[] numbers = { 28, 32, 14 }; int largest = numbers.Max(); // 32; Console.WriteLine(largest); } } The output: 32 If you include a selector expression: using System; using System.Collections; using System.Collections.Generic; using System.Linq; class Program { static void Main() { int[] numbers = { 28, 32, 14 }; int smallest = numbers.Max(n => n % 10); // 8; Console.WriteLine(smallest); } } The output: 8