CSharp - Dictionary<TKey,TValue> Type

Introduction

Dictionary implements both the generic and nongeneric IDictionary interfaces.

Demo

using System;
using System.Collections.Generic;
class MainClass/*from  w w w  . j av a2  s. c  o  m*/
{
   public static void Main(string[] args)
   {
     var d = new Dictionary<string, int>();

     d.Add("One", 1);
     d["Two"] = 2;     // adds to dictionary because "two" not already present
     d["Two"] = 22;    // updates dictionary because "two" is now present
     d["Three"] = 3;

     Console.WriteLine (d["Two"]);                
     Console.WriteLine (d.ContainsKey ("One"));   
     Console.WriteLine (d.ContainsValue (3));     

     int val = 0;

     if (!d.TryGetValue ("onE", out val))
       Console.WriteLine ("No val");              

     // Three different ways to enumerate the dictionary:

     foreach (KeyValuePair<string, int> kv in d)          
       Console.WriteLine (kv.Key + "; " + kv.Value);      

     foreach (string s in d.Keys) 
        Console.WriteLine (s);
     foreach (int i in d.Values) 
        Console.WriteLine(i);
   }
}

Result