Hi
First I wish u Happy Merry Christmas
pls explain about Dictionaries in c# collections
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
srinivasPosted Dec 24, 2009, 11:42 PM
The Dictionary Class
The Dictionary class stores key/value pairs. In a dictionary, values are accessed through their keys. In this regard it is similar to the non-generic Hashtable class. Dictionary implements both the generic and non-generic forms of IDictionary, ICollection, and IEnumerable. It also implements ISerializable and IDeserializationCallback. (The last two interfaces support the serialization of the list.) Dictionaries are dynamic, growing as needed.
Dictionary provides many constructors. Here is a sampling:
The first constructor creates an empty dictionary with a default capacity. The second creates a dictionary that contains the same elements as those in dict. The third lets you specify an initial capacity. If you know in advance that you will need a dictionary of a certain size, then specifying that capacity will prevent the resizing of the dictionary at runtime, which is a costly process.
Dictionary defines several methods. Some commonly used ones are shown in Table 23-16.
Method
Description
public void Add(TK k, TV v)
Adds the key/value pair specified by k and v to the dictionary. If the k is already in the dictionary, then its value is unchanged and an ArgumentException is thrown.
public bool ContainsKey(TK k)
Returns true if k is a key in the invoking dictionary. Returns false otherwise.
public bool ContainsValue(TV v)
Returns true if v is a value in the invoking dictionary. Returns false otherwise.
public IDictionary.Enumerator GetEnumerator( )
Returns an enumerator for the invoking dictionary.
public bool Remove(TK k)
Removes k from the dictionary. Returns true if successful. Returns false if k was not in the dictionary.
Dictionary defines the following properties:
Property
Description
public IEqualityComparer Comparer { get; }
Obtains the comparer for the invoking dictionary.
public int Count { get; }
Obtains the number of elements currently held by the invoking dictionary.
public Dictionary.KeyCollection Keys { get; }
Obtains a collection of the keys.
public Dictionary.ValueCollection Values { get; }
Obtains a collection of the values.
Notice that the keys and values contained within the collection are available as separate lists through the Keys and Values properties. The types Dictionary.KeyCollection and Dictionary.ValueCollection are collections that implement ICollection and IEnumerable in both their generic and non-generic forms.
Dictionary defines the following indexer:
public TV this[TK key] { get; set; }You can use this indexer to get or set the value of an element. You can also use it to add a new element to the collection. Notice that the "index" is not actually an index, but rather the key of the item.
Dictionary stores key/value pairs in the form of a KeyValuePair structure. Recall that this structure defines the following two fields:
These fields hold the key or value associated with an entry. Most of the time, you won't need to use KeyValuePair directly because Dictionary allows you to work the keys and values individually. However, when enumerating a Dictionary, such as in a foreach loop, the objects being enumerated are KeyValuePairs.
In a Dictionary , all keys must be unique, and a key must not change while it is in use as a key. Values need not be unique. The objects in a Dictionary are not stored in sorted order.
Here is an example that demonstrates Dictionary :
public static void Main() {dict.Add("Butler, John", 73000);dict.Add("Swartz, Sarah", 59000);dict.Add("Pyke, Thomas", 45000);dict.Add("Frank, Ed", 99000);Console.WriteLine("{0}, Salary: {1:C}", str, dict[str]);Here is the output:
srinivas reddyPosted Dec 24, 2009, 11:43 PM