1

If you can do this in List

List<int> a = new List<int>() {
   2, 4, 6, 8, 10
};

how can you do the same thing in Dictionary?

Dictionary<int, bool> b = new Dictionary<int, bool>() {
   ?, ?, ?
};
Robert Harvey
  • 173,679
  • 45
  • 326
  • 490
Jayson Ragasa
  • 981
  • 4
  • 17
  • 32
  • Possible duplicate of [Proper way to initialize a C# dictionary with values already in it?](https://stackoverflow.com/questions/17047602/proper-way-to-initialize-a-c-sharp-dictionary-with-values-already-in-it) – DavidRR Jul 19 '17 at 12:23

2 Answers2

8
Dictionary<int, bool> b = new Dictionary<int, bool>() {
   {1, true},{2, false},{3, true}
};
Josh
  • 44,334
  • 7
  • 99
  • 123
4

MSDN has an article on this: http://msdn.microsoft.com/en-us/library/bb531208.aspx

You just wrap each key-value pair in curly brackets.

Travis
  • 10,274
  • 1
  • 26
  • 46