Using C# LINQ - A Practical Overview

player_one
5,150 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Background Topics - IEnumerable<T>

The IEnumerable<T> interface is central to LINQ. All LINQ methods are extension methods to the IEnumerable<T> interface. That means that you can call any LINQ method on any object that implements IEnumerable<T>. You can even create your own classes that implement IEnumerable<T>, and those classes will instantly "inherit" all LINQ functionality!

Pretty cool huh?

What is IEnumerable<T>?

IEnumerable<T> is an interface that guarantees a given class is iterable. That's a technical term indicating a class that implements IEnumerable<T> can be thought of and used as a sequence of elements.

What classes implement IEnumerable<T>?

Most C# collections and all C# arrays implement IEnumerable<T>. Here are some examples:

IEnumerable<int> list = new List<int> { 1, 2, 3 };
IEnumerable<int> array = new[] { 1, 2, 3 };
IEnumerable<int> set = new SortedSet<int> { 1, 2, 3 };

What's the <T>?

A <T> after an interface name indicates that the interface is generic. This means that it can be used with any data type, and the T is a placeholder for that data type. In the case of IEnumerable<T>, the T represents the data type of the elements within the sequence.

An IEnumerable<int> contains a sequence of ints. An IEnumerable<string> contains a sequence of strings. An IEnumerable<object> contains (God help us) a sequence of objects, meaning it can hold, quite literally, anything.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content