DEV Community

manoj
manoj

Posted on

C# IEnumerables

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 and System.Collections.Generic namespace for generic collections.

Methods and Properties

  • The primary method in IEnumerable is GetEnumerator(), which returns an IEnumerator.
  • IEnumerator provides the necessary methods (MoveNext(), Reset()) and property (Current) to iterate over a collection.

Common Usage

  • IEnumerable is commonly used with foreach 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 an IEnumerable 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)