Mega Code Archive

 
Categories / C# Book / 04 LINQ
 

0395 Count

Input: IEnumerable<TSource> Optional expression: TSource => bool Count simply enumerates over a sequence, returning the number of items: using System; using System.Collections; using System.Collections.Generic; using System.Linq; class Program { static void Main() { int fullCount = new int[] { 5, 6, 7 }.Count(); // 3 Console.WriteLine(fullCount); } } The output: 3 You can optionally supply a predicate: using System; using System.Collections; using System.Collections.Generic; using System.Linq; class Program { static void Main() { int digitCount = "rntsoft.com".Count(c => char.IsDigit(c)); // 3 Console.WriteLine(digitCount); } }