Partitioning Operator - TakeWhile


The TakeWhile operator yields elements from a sequence while a test is true and then skips the remainder of the sequence.


The example uses TakeWhile to generate the sequence, then iterates over the sequence to print the values.


 








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 firstNumbersLessThan6 = numbers.TakeWhile(n => n < 6);


    Console.WriteLine("First numbers less than 6:");

     foreach (var n in firstNumbersLessThan6)

     {

       Console.WriteLine(n);

     }


     Console.ReadLine();

    }

  }

}





Output:


                    

Aspnet Related Tutorials

...more

New Aspnet Resources

...more

Copyright © 2012 VisualBuilder. All rights reserved