Tutorialspoint

C# Add key-value pair

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      IDictionary<int, string> d = new Dictionary<int, string>();
      
      d.Add(new KeyValuePair<int, string>(1, "TVs"));
      d.Add(new KeyValuePair<int, string>(2, "Appliances"));	
      d.Add(new KeyValuePair<int, string>(3, "Mobile"));
      d.Add(new KeyValuePair<int, string>(4, "Tablet"));	
      d.Add(new KeyValuePair<int, string>(5, "Laptop"));
      d.Add(new KeyValuePair<int, string>(6, "Desktop"));	
      d.Add(new KeyValuePair<int, string>(7, "Hard Drive"));
      d.Add(new KeyValuePair<int, string>(8, "Flash Drive"));	
				
      foreach (KeyValuePair<int, string> ele in d) {
         Console.WriteLine("Key = {0}, Value = {1}", ele.Key, ele.Value);
      }
   }
}

Advertisements
Loading...