Guest User

Untitled

a guest
Jun 17th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment