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.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay