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
Array.GetEnumerator Method in C#
The Array.GetEnumerator() method in C# is used to return an IEnumerator for the Array.
Syntax
Following is the syntax −
public virtual System.Collections.IEnumerator GetEnumerator ();
Example
Let us now see an example to implement the Array.GetEnumerator() method −
using System;
using System.Collections;
public class Demo{
public static void Main(){
int j = 0;
Console.WriteLine("Array elements...");
string[] arr = { "car", "bike", "truck", "bus"};
for (int i = 0; i < arr.Length; i++){
Console.Write("{0} ", arr[i]);
}
Console.WriteLine();
IEnumerator newEnum = arr.GetEnumerator();
Console.WriteLine("IEnumerator for the Array...");
while ((newEnum.MoveNext()) && (newEnum.Current != null)) {
Console.WriteLine("[{0}] {1}", j++, newEnum.Current);
}
}
}
Output
This will produce the following output −
Array elements... car bike truck bus IEnumerator for the Array... [0] car [1] bike [2] truck [3] bus
Advertisements