Partitioning Operator - Take


The Take operator yields a given number of elements from a sequence and then skips the remainder of the sequence.


This example uses Take to generate a sequence of the first three elements of an integer array. It then iterates through the sequence to print the results.








using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication4

{

class Program

{

  static void Main(string[] args)

   {

    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };


    var allButFirst4Numbers = numbers.Take(3);


    Console.WriteLine("First 3 Numbers");

     foreach (var n in allButFirst4Numbers)

      {

        Console.WriteLine(n);

      }


     Console.ReadLine();

     }

  }

}



Output:


                    

Aspnet Related Tutorials

...more

New Aspnet Resources

...more

Copyright © 2012 VisualBuilder. All rights reserved