Key Points:
What is IEnumerable?
-
IEnumerable
is an interface in C# that allows you to iterate over a collection of items. - It is found in the
System.Collections
namespace for non-generic collections andSystem.Collections.Generic
namespace for generic collections.
Methods and Properties
- The primary method in
IEnumerable
isGetEnumerator()
, which returns anIEnumerator
. -
IEnumerator
provides the necessary methods (MoveNext()
,Reset()
) and property (Current
) to iterate over a collection.
Common Usage
-
IEnumerable
is commonly used withforeach
loops to traverse collections such as arrays, lists, and other data structures. - It provides a standard way to iterate over any collection that implements the interface.
Examples of Methods
-
OfType<TResult>(IEnumerable)
: Filters the elements of anIEnumerable
based on a specified type. -
Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
: Concatenates two sequences. -
GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
: Groups the elements of a sequence according to a specified key selector function.
Top comments (0)