This is very common for beginners to get fuzzy with these words in their head. These are all alike and seem to be used for the same purpose. Well, let us see how all these are different.
Do they have the same purpose? In some ways, they do have the same purpose, but there are some differences also and we are going through the differences and similarities here in this article below.
Let us start with the types that start with an "I". They represent the interface, while their implementation are the classes that do not start with "I".
Interface |
Implementation |
IList |
List |
IQueryable |
Queryable |
IEnumerator |
|
IEnumarable |
Enumerable |
With the exception of the IQueryable / Queryable which derives from LINQ, they all derive from C# collections, along with many other types.
What is an interface?
An interface contains the signature of methods, properties, events, and indexers that are going to have their behavior implemented in the classes that inherit from it.
Example of an interface and its implementation -
- interface ISample
- {
- void AnyMethod();
- }
-
- class Sample : ISample
- {
-
- void AnyMethod()
- {
-
- }
-
- static void Main()
- {
-
- ISample obj = new Sample();
-
-
- obj.AnyMethod();
- }
- }
Benefits of using Interfaces
You may feel that it is a waste of time to create interfaces but here is why it is important.
- Decoupling, reducing dependency from implementation classes;
- Readability, as an interface implements the signature of your code their name must make sense with their purpose;
- Maintainability, making it possible of having different implementations based on the same interface;
- Extensibility, being easier to extend classes with a different implementation based on the same contract.
IQueryable vs IEnumarable vs IEnumerator vs IList
IQueryable
Inherits from IEnumerable and has this signature -
- using System.Collections;
- using System.Linq.Expressions;
-
- namespace System.Linq
- {
-
-
-
-
- public interface IQueryable : IEnumerable
- {
-
-
-
-
-
-
-
-
- Type ElementType { get; }
-
-
-
-
-
-
-
- Expression Expression { get; }
-
-
-
-
-
-
- IQueryProvider Provider { get; }
- }
- }
IEnumerable
Exposes the enumerator which supports a simple iteration over a collection of a specified type.
- namespace System.Collections.Generic
- {
-
-
-
-
-
-
-
-
- public interface IEnumerable<out T> : IEnumerable
- {
-
-
-
-
-
-
- IEnumerator<T> GetEnumerator();
- }
- }
IEnumerator
Supports a simple iteration over a generic collection and does not have an implementation like the other interfaces. It is better used for iteration over generic collections, like when using a foreach statement.
- namespace System.Collections.Generic
- {
-
-
-
-
-
-
-
- public interface IEnumerator<out T> : IEnumerator, IDisposable
- {
-
-
-
-
-
-
- T Current { get; }
- }
- }
A deeper explanation with a nice example can be found here.
IList
It inherits from ICollection and IEnumerable, representing a collection of objects that can be individually accessed by index.
- using System.Reflection;
-
- namespace System.Collections.Generic
- {
-
-
-
-
-
-
-
- [DefaultMember( "Item" )]
- public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- T this[int index] { get; set; }
-
-
-
-
-
-
-
-
-
-
-
- int IndexOf( T item );
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- void Insert( int index, T item );
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- void RemoveAt( int index );
- }
- }
Queryable vs Enumarable vs List
Queryable
- Namespace: System.LinQ;
- Implements and most of its methods extends IQueryable;
- Good for Query data in LinQ. Like to filter data;
- The query is deferred, being executed when those values are needed;
- Usage for LinQ to SQL
Enumerable
- Namespace: System.LinQ;
- Implements and most of its methods extends IEnumerable;
- Good for querying data from the in-memory collection, like list or array;
- The query is deferred, being executed when the object is enumerated.
- Usage for LINQ to Object and LINQ to XML;
List
- Namespace: System.Collections.Generic;
- Inherits from: ICollection<T>, IEnumerable<T>, IEnumerable, IList<T>, IReadOnlyCollection<T>, IReadOnlyList<T>, ICollection, IList;
- Good for interaction within in-memory objects. Like to update, remove or include items.
- The query is executed immediately, allocating the result in memory.
External References