Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to cast a type to its IEnumerable equivalent
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
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
Advertisements