Take operator outputs the first x elements, discarding the rest in CSharp

Description

The following code shows how to take operator outputs the first x elements, discarding the rest.

Example


using System;/*from  w ww .  j  a  va2  s  .com*/
using System.Collections.Generic;
using System.Linq;
public class MainClass {
    public static void Main() {

        int[] numbers = { 10, 9, 8, 7, 6 };
        IEnumerable<int> firstThree = numbers.Take(3);     // { 10, 9, 8 }
        foreach(int n in firstThree){
           Console.WriteLine(n);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ