TakeWhile method in C# ()

CsharpProgrammingServer Side Programming

With the TakeWhile() method, you can get methods by setting a condition base on Predicate.

Firstly, declare and initialize an array −

int[] arr = { 25, 40, 65, 70};

Now, use the TakeWhile() method and predicate to get all the elements that are less than 30.

var val = arr.TakeWhile(ele => ele < 30);

Let us see the same example, wherein we have displayed the values less than 30 using Predicate −

Example

 Live Demo

using System;
using System.Linq;
using System.IO;
public class Demo {
   public static void Main() {
      int[] arr = { 25, 40, 65, 70};
      var val = arr.TakeWhile(ele => ele < 30);
      foreach (int res in val) {
         Console.WriteLine(res);
      }
   }
}

Output

25
raja
Published on 05-Sep-2018 07:24:38
Advertisements