Saturday, February 17, 2018

Difference Between IEnumerable,ICollection and IList


IEnumerable interface contains only a single method definition i.e., GetEnumerator() and the GetEnumerator method must return an instance of an object of a class which implements the IEnumerator interface

Hide   Copy Code
public interface IEnumerable
IEnumerator GetEnumerator();
The IEnumerator interface implements two methods, MoveNext() and Reset() and it also has one property called Current that returns the current element in the list.


ICollection inherits IEnumerable apart from it contains some more method and count property

public interface ICollection : IEnumerable
int count { get; }
bool IsSynchronized { get; }
Object SyncRoot { get; }
void CopyTo(Array array,int index);

The IsSynchronized and SyncRoot properties help to make the collection thread-safe.


This is the generic version of ICollection Interface.

Defines methods to manipulate generic collections.

Hide   Copy Code
public interface ICollection : IEnumerable, IEnumerable
int count { get; }
bool IsReadOnly { get; }
void Add(T item);
void Clear();
bool Contains(T item);
void CopyTo(T[] array, it arrayIndex);
bool Remove(T item);


1. IList exist in System.Collections
2. IList Interface implements both IEnumerable and ICollection Interface.
3. IList is used to access an element in a specific position/index in a list.
4. Like IEnumerable, IList is also best to query data from in-memory collections like List, Array, etc.
5. IList is useful when you want to add or remove items from the list.
6. IList can find out the number of elements in the collection without iterating the collection.
7. IList supports deferred execution.
8. IList doesn’t support further filtering.

public interface IList : ICollection,IEnumerable
bool IsFixedSize { get; }
bool IsReadOnly { get; }
Object this[int index] { get;set; }
int Add(Object value);
int IndexOf(Object value);
void Insert(intindex,object value);
void Remove(Object value);
void RemoveAt(int index);

No comments: