Proper way to initialize a C# dictionary with values?

前端 未结 6 1580
感情败类
感情败类 2020-11-29 15:49

I am creating a dictionary in a C# file with the following code:

private readonly Dictionary FILE_TYPE_DICT
        = new Dictiona         


        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 15:58

    With C# 6.0, you can create a dictionary in following way:

    var dict = new Dictionary
    {
        ["one"] = 1,
        ["two"] = 2,
        ["three"] = 3
    };
    

    It even works with custom types.

提交回复
热议问题