Mega Code Archive

 
Categories / C# Book / 04 LINQ
 

0376 The Take operator outputs the first x elements, discarding the rest

using System; using System.Collections; using System.Collections.Generic; using System.Linq; class Program { static void Main() { int[] numbers = { 10, 9, 8, 7, 6 }; IEnumerable<int> firstThree = numbers.Take(3); foreach (int i in firstThree){ Console.WriteLine(i); } } } The output: 10 9 8