Adding Dictionary to a value of another Dictionary

I have the following scenario ::

public static class ServiceErrorCode
{
    public static IDictionary<string,Dictionary<int,string>> Mappings
    {
        get;
        private set;
    }
    static ServiceErrorCode()
    {
        Mappings = new Dictionary<string,Dictionary<int,string>>();
        Mappings.Add("Unauthorized", new Dictionary<int,string>().Add(103,"Test"));//Error is has some invalid arguments

}

Can anybody help how can i achieve the above and if i want to retrieve the inner dictionary values(both 101 and Test) using outer dictionary key (Unauthorized), then how can i achieve this ?

Please help . Thanks in advance.

Jon Skeet
people
quotationmark

The Add method returns void - so you can't use it as a method argument. However, you can use a collection initializer:

Mappings.Add("Unauthorized", new Dictionary<int,string> { { 103, "Test" } });

That's roughly equivalent to:

var tmp = new Dictionary<int,string>();
tmp.Add(103, "Test");
Mappings.Add("Unauthorized", tmp);

... but the collection initializer is easier to read, IMO.

people

See more on this question at Stackoverflow