TakeWhile method in C# ()


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

Updated on: 22-Jun-2020

355 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements