AsEnumerable() in C#


To cast a specific type to its IEnumerable equivalent, use the AsEnumerable() method. It is an extension method.

The following is our array −

int[] arr = new int[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;

Now, get the IEnumerable equivalent.

arr.AsEnumerable();

Example

 Live Demo

using System;
using System.Linq;
class Demo {
   static void Main() {
      int[] arr = new int[5];
      arr[0] = 10;
      arr[1] = 20;
      arr[2] = 30;
      arr[3] = 40;
      arr[4] = 50;
      var res = arr.AsEnumerable();
      foreach (var ele in res) {
         Console.WriteLine(ele);
      }
   }
}

Output

10
20
30
40
50

Updated on: 23-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements