C# Generic Collections Part 2: Dictionary

Dictionary:

Dictionary in C# is same as English dictionary. English dictionary is a collection of words and their definitions, often listed alphabetically in one or more specific languages. In the same way, the Dictionary in C# is a collection of Keys and Values, where key is like word and value is like definition.
Dictionary<TKey, TValue> is a generic collection included in the System.Collection.Generics namespace. TKey denotes the type of key and TValue is the type of TValue.

Code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4.   
  5. namespace dictionaryDemo  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             Dictionary<stringobject> dt = new Dictionary<stringobject>();  
  12.             dt.Add("Roll",1001);  
  13.             dt.Add("Name","Sujan Hasan");  
  14.             dt.Add("Dept","CSE");  
  15.             dt.Add("Email","sujanhasan@gmail.com");  
  16.             dt.Add("Age",21);  
  17.             print(dt);  
  18.          
  19.             Console.ReadKey();  
  20.         }  
  21.   
  22.         public static void print(Dictionary<stringobject> dictionary)  
  23.         {  
  24.             foreach (string x in dictionary.Keys)  
  25.             {  
  26.                 Console.WriteLine(x+" : "+dictionary[x]);  
  27.             }  
  28.         }  
  29.     }  
  30. }  
Share:

0 Comments:

Post a Comment