.Net

What is IEnumerable, IEnumerator, ICollection and IList,IQueryable in C#

IEnumerable

IEnumerable is an interface that defines one method GetEnumerator which returns an IEnumerator interface; this, in turn, allows read-only access to a collection.

The collection that implements IEnumerable can be used with a foreach statement.

IEnumerable enables you to iterate through the collection using a for-each loop.

So if your intention is just that, IEnumerable can help you achieve it with minimum effort (implementation of only one method – GetEnumerator()).

List, on the other hand, is a pre-implemented type-safe collection (using generic type) class available in the framework. This already has the implementation of IList, ICollection & IEnumerable.

So functionally IEnumerable is a subset of List. Also, List is a class whereas IEnumerable would need implementation.

IEnumerator 

IEnumerator allows you to iterate over a list, array, etc. (anything that implements IEnumerable) and process each element one-by-one.

If you’re familiar with a for-each loop in C#, IEnumerator is what it uses under the covers.

List<string> Items = new List<string>() {  "pen", "book", "bag"  }; 
foreach(string item in Items)
{
    Console.WriteLine(item);
}
List<string>Items =newList<string>(){"pen", "book", "bag" }; 
IEnumerator<string> enumerator =list.GetEnumerator(); 
while(enumerator.MoveNext())
{
stringitm= enumerator.Current; 
Console.WriteLine(item);
}

ICollection 

ICollection is an interface, you can’t instantiate it directly. You’ll need to instantiate a class that implements ICollection; for example, List<T>. Also, the ICollection interface doesn’t have an Add method — you’ll need something that implements IList or IList<T> for that.

List<object> icollection =newList<object>();
icollection.Add("items");

IList

Lists and arrays implement IList. This interface is an abstraction that allows list types to be used through a single reference type. With it, we can create a single method to receive an int[] or a List<int>.

First, with the IList generic interface, you must specify a type parameter. If you want your method to act upon ints, you can use IList<int>. Any type (string, object) can be specified.

I introduce the Display method, which receives an IList<int> parameter.

using System;
using System.Collections.Generic; 

class Program
{
static void Main() {
int[] array = new int[3];
array[0] = 1;
array[1] = 2;
array[2] = 3;
Display(array); 

List<int> list = new List<int>();
list.Add(5);
list.Add(7);
list.Add(9);
Display(list);
}
 
static void Display(IList<int> list) {
Console.WriteLine("Count: {0}", list.Count);
foreach (int value in list)
{
Console.WriteLine(value);
}
}
}

IQueryable

IQueryable exists in the System.Linq Namespace.

IQueryable is suitable for querying data from out-memory (like remote database, service) collections.

While querying data from a database, IQueryable executes a “select query” on server-side with all filters.

IQueryable is beneficial for LINQ to SQL queries.

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

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

Back to top button