Saturday 25 August 2012

Difference between IEnumerable, ICollection, IList, and IDictionary


Summary/Definitions:

All the below interfaces are parts of the System.Collections .Net assembly.

IEnumerable: Exposes the enumerator, which supports a simple iteration over a non-generic collection.

ICollection: Defines size, enumerators, and synchronization methods for all non-generic collections

IList: Represents a non-generic collection of objects that can be individually accessed by index.

IDictionary: Represents a non-generic collection of key/value pairs.

Following is the definitions of interfaces.

public interface IEnumerable

public interface ICollection : IEnumerable

public interface IList : ICollection, IEnumerable

public interface IDictionary : ICollection, IEnumerable

Differences between above assemblies are as follow.

Interface
Method(s)
Propertie(s)
IEnumerable
GetEnuerator()which will Returns an enumerator that iterates through a collection
-
ICollection
CopyTo(System.Array, int) method which will Copies the elements of the System.Collections.ICollection to an System.Array, starting at a particular System.Array index
Count (Gets the number of elements contained in the System.Collections.ICollection).
IsSynchronized (Gets a value indicating whether access to the System.Collections.ICollection is synchronized (thread safe)).
SyncRoot (Gets an object that can be used to synchronize access to the System.Collections.ICollection).
IList
Add(object) Adds an item to the System.Collections.IList, Clear()Removes all items from the System.Collections.IList.
IsFixedSize Gets a value indicating whether the System.Collections.IList has a fixed size.
Contains(object) Determines whether the System.Collections.IList contains a specific value.
IsReadOnly Gets a value indicating whether the System.Collections.IList is read-only.
IndexOf() Determines the index of a specific item in the System.Collections.IList
This[int] Gets or sets the element at the specified index.
Insert(int,object) Inserts an item to the System.Collections.IList at the specified index.
Remove(object) Removes the first occurrence of a specific object from the System.Collections.IList.
RemoveAt(int) Removes the System.Collections.
IDictionary
Add(object,object) Adds an element with the provided key and value to the System.Collections.IDictionary object.
IsFixedSize Gets a value indicating whether the System.Collections.IDictionary object has a fixed size.
Clear() Removes all elements from the System.Collections.IDictionary object.
IsReadOnly Gets a value indicating whether the System.Collections.IDictionary object is read-only.
Contains(object) Determines whether the System.Collections.IDictionary object contains an element with the specified key.
Keys Gets an System.Collections.ICollection object containing the keys of the System.Collections.IDictionary object.
GetEnuerator()Returns an System.Collections.IDictionaryEnumerator object for the System.Collections.IDictionary object.
This[object] Gets or sets the element with the specified key.
Remove(object) Removes the element with the specified key from the System.Collections.IDictionary object.
Values Gets a System.Collections.ICollection object containing the values in the System.Collections.IDictionary object.

So from above table we can conclude that IEnumerable interface is read-only collection. We can iterate through IEnumerable collection using yield also. 

ICollection is also read-only collection but we can copy collection into array and we have count, IsSyncronized etc. readonly properties on it. 

IList is collection where we can add, remove, search, and find data from collection with additional properties like IsFixedSize, IsReadonly etc. 

Where as IDictionary interface works same as IList but instead of integer index we have key, value pairs.

No comments:

Post a Comment

Custome Service Class to connect wcf service

  using  System; using  System.ServiceModel; namespace Custom.Service.Client {      // Borrowed from: http://old.iserviceoriented.com/blog/p...