Collections in C#3 min read

Today I’ll be writing about collections in C# and eventually in my next blog post I’ll be writing about some new cool feature in .NET Core 3.0.

But let’s first focus on collection basics in C#.

What is a collection?

It’s a container which can hold some items.

Types of collections

In case you don’t know, there are several types of collections in C#:

  • IEnumerable
  • IQueryable
  • ICollection
  • IList

Although they are basically the same, each of them has specific characteristics and is used in certain scenarios.

Collections in C#

As you can see in a picture above, IEnumerable is the starting class.
All other classes derive from it.

You might ask yourself, why IQueryable is moved left in the picture.
Why isn’t it located, as the rest of the collections, directly under the IEnumerable!?

Well mostly because, this way it’s easier to show you that IQueryable is located in a different namespace.

While IEnumerable, ICollection and IList is located in System.Collections, IQueryable is located inside System.Linq namespace.
I will explain later why!

System.Collections

IEnumerable

So as defined above, IEnumerable is a base class which represents a container. It’s located under System.Collections namespace.
Using this type of a collection you can not add, edit, delete or count items after initial load of items. This way we prevent any modification to our items. We use this container just to contain a list of items and nothing more. In order to count our elements inside this type of a collection, we need to implement IEnumerator.

i.e.:

public class BankAccount: IEnumerator 

{  

   public object Current 

   { 
​    get { throw new NotImplementedException(); } 

   } 

    public bool MoveNext() 

   { 

​    throw new NotImplementedException(); 

   } 

    public void Reset() 

   { 
​    throw new NotImplementedException(); 
   } 

} 

ICollection

This is a IEnumerable derived class with a little bit more functionalities added in it. Using this class we are able to add, edit, delete and count elements in the container.

You will mostly see this kind of collection when dealing with Entity Framework. Since it doesn’t have so much overhead.

IList

This type of collection derives from ICollection. Said that, it means, it has all specifics of IEnumerable and ICollection plus some additional functionalities like insert or delete of an element in the middle of a list. It’s the easiest collection and probably the most used type of a collection. But it’s overhead makes it not performance king.

IQueryable

And finally we have this kind of a collection. And the main difference between this collection and the other is that when accessing database it generates LINQ to SQL expression which is executed over the database layer.

Final verdict: IEnumerable vs IQueryable

There are some key differences between these two types of collections. I will sum-up them in a table below.

But one of the biggest differences which is necessary to grasp is the way each of the two queries data.

While IEnumrable is suitable for querying in-memory collections like arrays,lists etc, IQueryable is suitable for querying out-memory colelctions like some service,remote databases etc.

Why?

When using IEnumerable to query database, we execute “select query” on the server side then load that data into in-memory on the client side and only then we filter data.

On the other side, when using IQueryable we execute “select query” with all the filters on the server side and return already filtered data.

As you can see it’s much performance-wise to use IQueryable when retrieving filtered data from a database.

IEnumerableIQueryable
Namespace: System.CollectionsNamespace: System.Linq
Queries: IN-MEMORYQueries: OUT-MEMORY
DB: “Select query” on the server, filters on the client side DB: “Select query” and filters on the server side
Beneficial: LINQ to OBJECT or LINQ to XMLBeneficial: LINQ to SQL

Leave a Reply

Your email address will not be published. Required fields are marked *