Dictionary represent a collection of keys and values pair of data.
The Dictionary class is a generic class and can store any data types.
The Dictionary class constructor takes two parameters (generic type), first for the type of the key and second for the type of the value.
Dictionary Example program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace iDictionary
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, Int32> sampleDic = new Dictionary<string, int>();
sampleDic.Add("Babu", 1111);
sampleDic.Add("Prabhu", 2222);
sampleDic.Add("Harshad",2223);
Console.WriteLine("IDictionary Value");
Console.ReadKey();
foreach (KeyValuePair<string, Int32> item in sampleDic)
{
Console.WriteLine("Key = {0} ,Value = {1}", item.Key, item.Value);
}
Console.ReadKey();
Console.WriteLine("Thanks");
Console.ReadKey();
Console.WriteLine("----------------");
Console.WriteLine("Dictionary Prperties");
Console.WriteLine("Count: {0}", sampleDic.Count);
Console.WriteLine("----------------");
Console.WriteLine("Dictionary KeyCollection Prperties");
Dictionary<string, Int32>.KeyCollection keys = sampleDic.Keys;
foreach (string key in keys)
{
Console.WriteLine("Key : {0}", key);
}
Console.WriteLine("----------------");
Console.WriteLine("Dictionary ValueCollection properties ");
Dictionary<string, Int32>.ValueCollection val = sampleDic.Values;
foreach (Int32 item in val)
{
Console.WriteLine("Value : {0}", item);
}
Console.ReadKey();
}
}
}