TakeWhile

Input: IEnumerable<TSource>
Lamdda expression:TSource => bool or (TSource,int) => bool

TakeWhile enumerates the input sequence, emitting each item, until the given predicate is false.

It then ignores the remaining elements:

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        int[] numbers = { 3, 5, 2, 7, 4, 1 };
        var takeWhileSmall = numbers.TakeWhile(n => n < 5);
        foreach(int s in takeWhileSmall){
           Console.WriteLine(s);
        }
    }
}

The output:


3

TakeWhile with lambda

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {

        string[] names = { "Java", "C#", "Javascript", "SQL", "Oracle", "Python", "C++", "C", "HTML", "CSS" };

        IEnumerable<string> query = names.TakeWhile(n => n.Length < 5);
        foreach(String s in query){
           Console.WriteLine(s);
        }
    }
}

The output:


Java
C#
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.