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
  • 178,213
  • 47
  • 333
  • 501
Jayson Ragasa
  • 1,011
  • 4
  • 19
  • 33
  • 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,706
  • 7
  • 102
  • 124
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,444
  • 2
  • 28
  • 48