Monday 17 December 2012

c sharp programming

The DictionaryEntry Structure
System.Collections defines one structure type called DictionaryEntry. Non-generic
collections that hold key/value pairs store those pairs in a DictionaryEntry object. This
structure defines the following two properties:
public object Key { get; set; }
public object Value { get; set; }
These properties are used to access the key or value associated with an entry. You can
construct a DictionaryEntry object by using the following constructor:
public DictionaryEntry(object k, object v)
Here, k is the key and v is the value.




A dictionary is a collection in which each element is a key/value pair. Dictionaries
are most commonly used for lookups and sorted lists.
The  Framework  defines  a  standard  protocol  for  dictionaries,  via  the  interfaces
IDictionary and IDictionary <TKey, TValue>, as well as a set of general-purpose
dictionary classes. The classes each differ in the following regard:
• Whether or not items are stored in sorted sequence
• Whether or not items can be accessed by position (index) as well as by key
• Whether generic or nongeneric
• Their performance when large



IDictionary<TKey,TValue>
IDictionary<TKey,TValue> defines the standard protocol for all key/value-based col-
lections. It extends ICollection<T> by adding methods and properties to access el-
ements based on a key of arbitrary type:
public interface IDictionary <TKey, TValue> :
  ICollection <KeyValuePair <TKey, TValue>>, IEnumerable
{
   bool ContainsKey (TKey key);
   bool TryGetValue (TKey key, out TValue value);
   void Add         (TKey key, TValue value);
   bool Remove      (TKey key);
   TValue this [TKey key]      { get; set; }  // Main indexer - by key
   ICollection <TKey> Keys     { get; }       // Returns just keys
   ICollection <TValue> Values { get; }       // Returns just values
}
To add an item to a dictionary, you either call Add or use the index’s set accessor—
the latter adds an item to the dictionary if the key is not already present (or updates
the item if it is present). Duplicate keys are forbidden in all dictionary implementa-
tions, so calling Add twice with the same key throws an exception.
To  retrieve  an  item  from  a dictionary, use  either  the  indexer or  the  TryGetValue
method. If the key doesn’t exist, the indexer throws an exception whereas TryGet
Value returns false. You can test for membership explicitly by calling ContainsKey;
however, this incurs the cost of two lookups if you then subsequently retrieve the
item.


Enumerating  directly  over  an  IDictionary<TKey,TValue>  returns  a  sequence  of
KeyValuePair structs:
public struct KeyValuePair <TKey, TValue>
{
  public TKey Key     { get; }
  public TValue Value { get; }
}
You  can  enumerate over  just  the keys or  values  via  the dictionary’s  Keys/Values
properties.
We demonstrate the use of this  interface with the generic Dictionary class  in the
following section.

No comments:

Post a Comment