How to insert values into C# Dictionary on instantiation?

C#Dictionary

C# Problem Overview


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")};

C# Solutions


Solution 1 - C#

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}}
};

Solution 2 - C#

Dictionary<int, string> dictionary = new Dictionary<int, string> { 
   { 0, "string" }, 
   { 1, "string2" }, 
   { 2, "string3" } };

Solution 3 - C#

You were almost there:

var dict = new Dictionary<int, string>()
{ {0, "string"}, {1,"string2"},{2,"string3"}};

Solution 4 - C#

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.

Solution 5 - C#

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 :)

Solution 6 - C#

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

var dictionary = new Dictionary<int, string>
    {
        {0, "string"},
        {1, "string2"},
        {2, "string3"}
    };

Solution 7 - C#

Hope it will work perfectly.

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

Solution 8 - C#

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"];
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionkd7iwpView Question on Stackoverflow
Solution 1 - C#Daniel EarwickerView Answer on Stackoverflow
Solution 2 - C#Timothy CarterView Answer on Stackoverflow
Solution 3 - C#Henk HoltermanView Answer on Stackoverflow
Solution 4 - C#matendieView Answer on Stackoverflow
Solution 5 - C#russelrillemaView Answer on Stackoverflow
Solution 6 - C#JosephView Answer on Stackoverflow
Solution 7 - C#user4410190View Answer on Stackoverflow
Solution 8 - C#JacksonkrView Answer on Stackoverflow