C# .NET - diffrence between ienurable ilist idictionary collection

Asked By shoaib mansoori on 17-May-11 06:57 AM
Hi all
diffrence between ienurable ilist idictionary collection and when to use what means in which senario we use ienurable ilist idictionary
please help me
thanks
regards
Shoaib
Jitendra Faye replied to shoaib mansoori on 17-May-11 06:59 AM

All of these interfaces inherit from IEnumerable, which you should make sure you understand. That interface basically lets you use the class in a foreach statement (in C#).

  • ICollection is the most basic of the interfaces you listed. It's an enumerable interface that supports a Count and that's about it.
  • IList is everything that ICollection is, but it also supports adding and removing items, retrieving items by index, etc. It's the most commonly-used interface for "lists of objects", which is vague I know.
  • IQueryable is an enumerable interface that supports LINQ. You can always create an IQueryable from an IList and use LINQ to Objects, but you also find IQueryable used for deferred execution of SQL statements in LINQ to SQL and LINQ to Entities.
  • IDictionary is a different animal in the sense that it is a mapping of unique keys to values. It is also enumerable in that you can enumerate the key/value pairs, but otherwise it serves a different purpose than the others you listed.

The MSDN documentation for each of these is decent, so I would start there in rounding out your understanding.

Reena Jain replied to shoaib mansoori on 17-May-11 07:03 AM
hi,

IDictionary is for key->value maps, ICollection is for sets of similar objects.


ICollection is an interface for collections of similar objects: the controls on a form, the elements in a list, the attributes in an XML tag, and so on. As of .NET 2.0, there’s a generic version, so you can refer to a collection of integers as ICollection<int>.

IDictionary is an interface for mapping one type of object or value to another. It works like a real dictionary, or a phone book: you have a "key" in mind like a person’s name, and when you look it up, you get some information that’s identified by that key, like an address or phone number. Each key can only be listed once, although two different keys are still allowed to have the same value. This is also generic in .NET 2.0, so a dictionary whose keys are strings and whose values are integers would be IDictionary<string,int>.

A dictionary is actually a collection of key/value pairs: you can use an IDictionary<int,string> as an ICollection<KeyValuePair<int,string>>, and you can access the keys and values as separate collections with the Keys and Values properties.

Both ICollection and IDictionary are unordered, meaning that although you can retrieve the elements in some order with the CopyTo method or a foreach loop, that order has no special meaning, and it might change for no apparent reason. That’s the main difference between ICollection and IList: a list lets you put items in specific positions, just like an array, and they stay there until you move them.

Finally, note that these are all interfaces, not classes. .NET gives you a few different types of collections, lists, and dictionaries to choose from, and the interfaces like ICollection and IDictionary give you a common way to access all of them without worrying about the details. When you create one in your own code, you’ll usually want to use the generic List or Dictionary classes.

Collections simply hold values. Dictionaries have values indexed by keys.


hope this will help you
Anoop S replied to shoaib mansoori on 17-May-11 07:17 AM
IDictionary Interface Represents a nongeneric collection of key/value pairs.

public interface IDictionary : ICollection,
    IEnumerable

he IDictionary interface is the base interface for nongeneric collections of key/value pairs. For the generic version of this interface, see System.Collections.Generic.IDictionary<TKey, TValue>.

Each element is a key/value pair stored in a DictionaryEntry object.

Each pair must have a unique key. Implementations can vary in whether they allow the key to be null. The value can be null and does not have to be unique. The IDictionary interface allows the contained keys and values to be enumerated, but it does not imply any particular sort order.

refer this for more details
http://msdn.microsoft.com/en-us/library/system.collections.idictionary.aspx

ICollection: add a few elements to the collection
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

public class MainClass
{

    public static void Main()
    {
      ICollection<int> myCollection = new Collection<int>();

      myCollection.Add(105);
      myCollection.Add(232);
      myCollection.Add(350);
    }
}
Riley K replied to shoaib mansoori on 17-May-11 07:33 AM

Dictionaries and hashtables both map a key to a value, whereas a list just contains a collection of values.  The difference between a dictionary and a hashtable is an implementation detail.  In a hashtable, the key of an item is the result of calling GetHashCode() on the value, whereas with a dictionary, the key must be supplied by you.  Keys in a dictionary are sorted so they can be found (relatively) quickly.  The Dictionary<TKey,TValue> class uses a hash table internally.

  Hashtable is basically obselete since .NET 2.0.  You should always be using Dictionary instead of Hashtable.

So really it comes down to list vs. dictionary.  Do the items you want to put into a collection have some kind of unique identifier that could be used to sort them so they can be stored & retrieved faster?  If the answer is yes, use Dictionary.  If the answer is no, use List.