Pages

Aug 31, 2012

Difference among IEnumerable , IQueryable, ICollection,IList, List


  1. IEnumerable (most basic)
  2. IEnumerable-> IQueryable<T>
  3. IEnumerable->  List<T> (just an output format, and while it implements IEnumerable<T>, is not directly related to querying or IQueryable. )
  4. IEnumerable->  ICollection<T>
  5. IEnumerable-> ICollection<T> -> IDictionary (An IDictionary implementation is a collection of key-and-value pairs, like theHashtable class. )
  6. IEnumerable-> ICollection<T> -> IList (An IList implementation is a collection of values that can be sorted and whose members can be accessed by index, like the ArrayList class. In addition, IList implementations fall into three categories: read-only, fixed-size, variable-size. A read-only IList cannot be modified. A fixed-size IList does not allow the addition or removal of elements, but it allows the modification of existing elements. A variable-size IList allows the addition, removal and modification of elements.)
  7. If neither the IDictionary interface nor the IList interface meet the requirements of the required collection, derive the new collection class from the ICollection interface instead for more flexibility.
  8. Some collections that limit access to their elements, like the Queue class and the Stack class, directly implement the ICollection interface.

Approaches/Facilities List of  IEnumerable , IQueryableICollection,IList, List:


OR

Difference among  IEnumerable<T> , IQueryable<T>, ICollection<T>,IList<T>, List<T>:


In this topic I am going discuss about IEnumerable<T> , Iqueryable<T> , ICollection<T>, IList<T>, List<T> generic’s pros and cons with fine tune differences in use of terms.IEnumerable<T> , Iqueryable<T> , ICollection<T>, IList<T>, List<T> are collected from various sites like stackoverflow , msdn etc. This topic is a general summarization of those generics objects.


Pros
1.       The most generic item of all.
2.       Still might use deferred execution.
3.       IEnumerable is best suitable for working with queries later on. It helps best when we try to add more queries with our previous one and we do not want to run our previous query in realtime but when we requested it or iterated through those or finally passing as a .ToList() object .
4.       IEnumerable does not run query until it is requested by iteration.




Cons

1.       IEnumerable doesn’t move between items, it is forward only collection as LinkedList. You can't get at "item 4" without passing items 0-3.
2.       Read-only list, you can't add to it or remove from it.
3.       Actually the model binder is not able to bind an enumerable because it is a too generic Interface, and it is not able to choose a concrete class to create that implements the IEnumerable.



Pros
1.       Query isn't executed until to really iterate over the items, maybe by doing a .ToList()
2.       IQueryable best suits for remote data source, like a database or web service. IQueryable is a very powerful feature that enables a variety of interesting deferred execution scenarios (like paging and composition based queries).

Cons

1.       avoid passing IQueryable to Views
2.       It is not a good place to handle errors...so better getting rid of delayed execution before passing to Views.

Is between IEnumerable and IList. In addition, is the base of IList and IDictionary Objects.

Pros
1.       Considered the most basic type for collections.
2.       The System.Xml.Serialization.XmlSerializer class has specific requirements for types that implement ICollection and System.Collections.IEnumerable in order to be serializable.
3.      Inherited from IEnumerable<T>.
4.      Collection<T> , IList<T>, IDictionary all are inherited from this, so we have flexibility.
5.      Base of IList and IDictionary Object.
6.      Add, Delete and modify items in the collection is acceptable.
7.       Some collections that limit access to their elements, like the Queue class and the Stack class, directly implement the ICollection interface.
8.       Normally in EF table relationships we use this ICollection in virtual keyword.
Cons
1.       ICollection doesn’t have indexing like IList does.

Pros
1.       Random access to the full list(like an ArrayList)
2.       Entirely in memory
3.      Best for model binding.
4.      Supports adding and removing but for only variable-size one.
5.       An IList implementation is a collection of values that can be sorted and whose members can be accessed by index, like the ArrayList class.
6.       In addition, IList implementations fall into three categories:
a)       Read-only: (A read-only IList cannot be modified)
b)      Fixed-size: (A fixed-size IList does not allow the addition or removal of elements, but it allows the modification of existing elements.)
c)       Variable-size: (A variable-size IList allows the addition, removal and modification of elements.)




Cons

1.       Memory Consuming.


Propertise
1.      Implements : IList<T>, ICollection<T>, IEnumerable<T>, IListICollectionIEnumerable
Pros

1.      The List<T> class is the generic equivalent of the ArrayList class. It implements the IList<T> generic interface using an array whose size is dynamically increased as required.
2.      In deciding whether to use the List<T> or ArrayList class, both of which have similar functionality, remember that the List<T> class performs better in most cases and is type safe.
3.       Methods such as BinarySearch and Sort use an ordering comparer for the list elements. The default comparer for type T is determined as follows. If type T implements the IComparable<T> generic interface, then the default comparer is the CompareTo(T) method of that interface; otherwise, if type T implements the nongeneric IComparable interface, then the default comparer is the CompareTo(Object) method of that interface. If type T implements neither interface, then there is no default comparer, and a comparer or comparison delegate must be provided explicitly.

Cons

1.      List<T> accepts null as a valid value for reference types and allows duplicate elements.
2.       The List<T> is not guaranteed to be sortedYou must sort the List<T> before performing operations (such as BinarySearch) that require the List<T> to be sorted.

READONLYCOLLECTION<T>
Propertise
1.       Implements : IList<T>,   ICollection<T>, IEnumerable<T>, IListICollectionIEnumerable
Example
List<string> dinosaurs = new List<string>();

dinosaurs.Add("Tyrannosaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Deinonychus");
dinosaurs.Add("Compsognathus");

ReadOnlyCollection<string> readOnlyDinosaurs =
            new ReadOnlyCollection<string>(dinosaurs);

SUMMARY

Non-Generic                       
Similar Generic Type
ArrayList
List<T>
Hashtable
Dictionary<TKey,TValue>
SortedList
SortedList<TKey,TValue>
Queue
Queue<T>
Stack 
Stack<T>
IEnumerable
IEnumerable<T>
ICollection
IEnumerable<T>  and System.Xml.Serialization.XmlSerializer
N/A                   
ICollection<T>
IList
IList<T>
CollectionBase
Collection<T>
ReadOnlyCollectionBase
ReadOnlyCollection<T>
DictionaryBase
N/A (just implement IDictionary<TKey,TValue>
N/A                   
SortedDictionary<TKey,TValue>
N/A                   
KeyedCollection<TKey,TItem>
N/A                   
LinkedList<T>

1 comment: