Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 1.03 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How do I copy the content of a dictionary to an new dictionary in C#?
  2. Dictionary<string, string> d = new Dictionary<string, string>();
  3. Dictionary<string, string> d2 = new Dictionary<string, string>(d);
  4.        
  5. Dictionary<string, string> d = new Dictionary<string, string>();
  6. Dictionary<string, string> d2 = d;
  7.        
  8. using System;
  9. using System.Collections.Generic;
  10.  
  11. class Program
  12. {
  13.     static void Main(string[] args)
  14.     {
  15.         Dictionary<string, string> first = new Dictionary<string, string>()
  16.         {
  17.             {"1", "One"},
  18.             {"2", "Two"},
  19.             {"3", "Three"},
  20.             {"4", "Four"},
  21.             {"5", "Five"},
  22.             {"6", "Six"},
  23.             {"7", "Seven"},
  24.             {"8", "Eight"},
  25.             {"9", "Nine"},
  26.             {"0", "Zero"}
  27.         };
  28.  
  29.         Dictionary<string, string> second = new Dictionary<string, string>();
  30.         foreach (string key in first.Keys)
  31.         {
  32.             second.Add(key, first[key]);
  33.         }
  34.  
  35.         first["1"] = "newone";
  36.         Console.WriteLine(second["1"]);
  37.     }
  38. }