Posted By

sureshdasari on 10/29/18


Tagged

csharp programming collections dotnet visualStudio tutlane


Versions (?)

C# Dictionary with Examples


 / Published in: C#
 

URL: https://www.tutlane.com/tutorial/csharp/csharp-dictionary

In c#, Dictionary is a generic type of collection and it is used to store a collection of key / value pairs that are organized based on the key. The dictionary in c# will allow to store only the strongly typed objects i.e. the key / value pairs of specified data type.

  1. using System;
  2.  
  3. using System.Collections.Generic;
  4.  
  5.  
  6.  
  7. namespace Tutlane
  8.  
  9. {
  10.  
  11. class Program
  12.  
  13. {
  14.  
  15. static void Main(string[] args)
  16.  
  17. {
  18.  
  19. //Create a new dictionary
  20.  
  21. Dictionary<int, string> dct = new Dictionary<int, string>();
  22.  
  23. dct.Add(1, "Suresh");
  24.  
  25. dct.Add(4, "Rohini");
  26.  
  27. dct.Add(2, "Trishi");
  28.  
  29. dct.Add(3, "Praveen");
  30.  
  31. dct.Add(5, "Sateesh");
  32.  
  33. Console.WriteLine("Contains Key 2: {0}", dct.ContainsKey(2));
  34.  
  35. Console.WriteLine("Contains Value 'Tutlane': {0}", dct.ContainsValue("Tutlane"));
  36.  
  37. Console.ReadLine();
  38.  
  39. }
  40.  
  41. }
  42.  
  43. }

Report this snippet  

You need to login to post a comment.