C# Program to cast a type to its IEnumerable equivalent

CsharpProgrammingServer Side Programming

Use the AsEnumerable() method to cast a type to its IEnumerable equivalent. It is an extension method.

For our example, we have set an array.

int[] myArr = new int[10];
myArr[0] = 1;
myArr[1] = 2;
myArr[2] = 3;
myArr[3] = 4;
myArr[4] = 5;

Now, we have used the AsEnumerable() method to cast.

myArr.AsEnumerable();

Example

 Live Demo

using System;
using System.Linq;
class Demo {
   static void Main() {
      int[] myArr = new int[10];
      myArr[0] = 1;
      myArr[1] = 2;
      myArr[2] = 3;
      myArr[3] = 4;
      myArr[4] = 5;
      myArr[5] = 6;
      myArr[6] = 7;
      myArr[7] = 8;
      myArr[8] = 9;
      myArr[9] = 10;

      // AsEnumerable
      var a = myArr.AsEnumerable();

      // Displaying
      foreach (var item in a) {
         Console.WriteLine(item);
      }
   }
}

Output

1
2
3
4
5
6
7
8
9
10
raja
Published on 12-Sep-2018 08:30:08
Advertisements