Mega Code Archive

 
Categories / C# Book / 04 LINQ
 

0379 Aggregation operators

The Aggregation operators return a scalar value; usually of numeric type: using System; using System.Collections; using System.Collections.Generic; using System.Linq; class Program { static void Main() { int[] numbers = { 10, 9, 8, 7, 6 }; int count = numbers.Count(); // 5; Console.WriteLine(count); int min = numbers.Min(); // 6; Console.WriteLine(min); } } The output: 5 6 The quantifiers return a bool value: using System; using System.Collections; using System.Collections.Generic; using System.Linq; class Program { static void Main() { int[] numbers = { 10, 9, 8, 7, 6 }; bool hasTheNumberNine = numbers.Contains(9); // true bool bool hasMoreThanZeroElements = numbers.Any(); // true bool bool hasAnOddElement = numbers.Any(n => n % 2 == 1); // true } }