What does the interface IEnumerable do in C#?



IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface. It is the base interface for all non-generic collections that can be enumerated.

This works for read-only access to a collection that implements that IEnumerable can be used with a foreach statement.

It has a single method −

  • GetEnumerator() − This method returns an enumerator that iterates through a collection.

The following is the implementation of the GetEnumerator() method of the IEnumerable interface in C# −

IEnumerator IEnumerable.GetEnumerator() {
return (IEnumerator) GetEnumerator();
}

The following are the extension methods of the IEnumerable interface in C# −

Sr.No Method Name & Description
1 AsParallel()
Enables parallelization of a query
2 AsQueryable()
The method converts an IEnumerable to an IQueryable.
3 Cast<TResult>()
The method casts the elements of an IEnumerable to the specified type
4 OfType<TResult>()
Filters the elements of an IEnumerable based on a specified type.
Updated on: 2020-06-20T15:22:15+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements