158

Does anyone know if there is a way I can insert values into a C# Dictionary when I create it? I can, but don't want to, do dict.Add(int, "string") for each item if there is something more efficient like:

Dictionary<int, string>(){(0, "string"),(1,"string2"),(2,"string3")};
cgp
  • 41,026
  • 12
  • 101
  • 131
kd7iwp
  • 3,110
  • 8
  • 30
  • 39
  • 1
    possible duplicate of [Can I use a collection initializer for Dictionary entries?](http://stackoverflow.com/questions/495038/can-i-use-a-collection-initializer-for-dictionarytkey-tvalue-entries) – Chris Moschini Sep 27 '13 at 20:08

8 Answers8

217

There's whole page about how to do that here:

http://msdn.microsoft.com/en-us/library/bb531208.aspx

Example:

In the following code example, a Dictionary<TKey, TValue> is initialized with instances of type StudentName:

var students = new Dictionary<int, StudentName>()
{
    { 111, new StudentName {FirstName="Sachin", LastName="Karnik", ID=211}},
    { 112, new StudentName {FirstName="Dina", LastName="Salimzianova", ID=317}},
    { 113, new StudentName {FirstName="Andy", LastName="Ruth", ID=198}}
};
Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
49
Dictionary<int, string> dictionary = new Dictionary<int, string> { 
   { 0, "string" }, 
   { 1, "string2" }, 
   { 2, "string3" } };
Timothy Carter
  • 15,459
  • 7
  • 44
  • 62
14

You were almost there:

var dict = new Dictionary<int, string>()
{ 
   {0, "string"}, 
   {1, "string2"},
   {2, "string3"}
};
H H
  • 263,252
  • 30
  • 330
  • 514
11

You can also use Lambda expressions to insert any Key Value pairs from any other IEnumerable object. Key and value can be any type you want.

Dictionary<int, string> newDictionary = 
                 SomeList.ToDictionary(k => k.ID, v => v.Name);

I find that much simpler since you use the IEnumerable objects everywhere in .NET

Hope that helps!!!

Tad.

matendie
  • 693
  • 7
  • 11
9

Just so you know as of C# 6 you can now initialize it as follows

var students = new Dictionary<int, StudentName>()
{
    [111] = new StudentName {FirstName="Sachin", LastName="Karnik", ID=211},
    [112] = new StudentName {FirstName="Dina", LastName="Salimzianova", ID=317},
    [113] = new StudentName {FirstName="Andy", LastName="Ruth", ID=198}
};

Much cleaner :)

russelrillema
  • 452
  • 7
  • 14
7

You can instantiate a dictionary and add items into it like this:

var dictionary = new Dictionary<int, string>
    {
        {0, "string"},
        {1, "string2"},
        {2, "string3"}
    };
Joseph
  • 25,330
  • 8
  • 76
  • 125
-1

Hope it will work perfectly.

Dictionary<string, double> D =new Dictionary<string, double>(); D.Add("String", 17.00);

  • Hi, welcome to SO! The person who originally asked this question specifically said that they don't want to do the `.Add(int, "string")` method of adding values to a dictionary. Sorry but this doesn't answer the question. – Kezz Mar 15 '15 at 19:22
-2

This isn't generally recommended but in times of uncertain crises you can use

Dictionary<string, object> jsonMock = new Dictionary<string, object>() { { "object a", objA }, { "object b", objB } };

// example of unserializing
ClassForObjectA anotherObjA = null;
if(jsonMock.Contains("object a")) {
    anotherObjA = (ClassForObjA)jsonMock["object a"];
}
Jacksonkr
  • 31,583
  • 39
  • 180
  • 284