How can I initialize a global nested dictionary around the world?

I have my dictionary declared as follows:

private static Dictionary<string, Dictionary<string, string>> myDictionary;

I want to initialize it globally. The closest I arrived initializes the external dictionary, but still leaves me with a null reference to the internal dictionary:

private static Dictionary<string, Dictionary<string, string>> myDictionary
 = new Dictionary<string, Dictionary<string, string>>();

I need not to initialize it in the method, because I do not want to force the user to call the method before using my class. The user has access only to static methods. I could create a singleton when they call one of the methods, but this one is dirty.

How can I declare both dictionaries globally? Something along the lines of one of them (although not compiled):

private static Dictionary<string, Dictionary<string, string>> myDictionary
 = new Dictionary<string, new Dictionary<string, string>>();

or

private static Dictionary<string, string> inner = new Dictionary<string, string>();
private static Dictionary<string, Dictionary<string, string>> myDictionary
 = new Dictionary<string, inner>();
+4
2

( , myDictionary MyClass):

public class MyClass
{
    private static Dictionary<string, Dictionary<string, string>> myDictionary;

    static MyClass()
    {
        //Initialize static members here
        myDictionary = new Dictionary<string, Dictionary<string, string>>();
        myDictionary.Add("mykey", new Dictionary<string, string>());
        ...
    }
}

, , .

+5

inline, , {}:

private static Dictionary<string, Dictionary<string, string>> = new Dictionary<string, Dictionary<string, string>> {
    {   "first"
    ,   new Dictionary<string, string> {
            {"A", "one"}
        ,   {"B", "two"}
        }
    }
,   {   "second"
    ,   new Dictionary<string, string> {
            {"Y", "twenty five"}
        ,   {"Z", "twenty six"}
        }
    }
}
+4

All Articles