Key Points:
What is IEnumerable?
-
IEnumerableis an interface in C# that allows you to iterate over a collection of items. - It is found in the
System.Collectionsnamespace for non-generic collections andSystem.Collections.Genericnamespace for generic collections.
Methods and Properties
- The primary method in
IEnumerableisGetEnumerator(), which returns anIEnumerator. -
IEnumeratorprovides the necessary methods (MoveNext(),Reset()) and property (Current) to iterate over a collection.
Common Usage
-
IEnumerableis commonly used withforeachloops 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 anIEnumerablebased 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)