设置私人字典

Jon*_*ing 2 c# dictionary

我有一个工作的例子Dictionary<string, int>.我需要设置Dictionary为a private Dictionary并检查值.

目前我有:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    Dictionary<string, int> docTypeValue = new Dictionary<string, int>();
    docTypeValue.Add("OSD", 1);
    docTypeValue.Add("POD", 2);
    docTypeValue.Add("REC", 3);
    docTypeValue.Add("CLAINF", 4);
    docTypeValue.Add("RATE", 5);
    docTypeValue.Add("OTHER", 6);
    docTypeValue.Add("CARINV", 7);
    docTypeValue.Add("PODDET", 8);
    docTypeValue.Add("BOLPO", 9);

    litText.Text = docTypeValue[txtDocType.Text].ToString();
}
Run Code Online (Sandbox Code Playgroud)

这按预期工作.我需要使用房产吗?即下面

private Dictionary<string, int> DocTypeValue
{
    get;
    set; 
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能重构上面的内容来创建建议private Dictionary

Sri*_*vel 5

你正在寻找这样的东西.使用Collection Initializer功能.

private Dictionary<string, int> docTypeValue = new Dictionary<string, int> 
{ 
    { "OSD", 1 },
    {"POD", 2},
    {"REC", 3},
    {"CLAINF", 4},
    //...
};
Run Code Online (Sandbox Code Playgroud)