Proper way to initialize a C# dictionary with values?

前端 未结 6 1579
感情败类
感情败类 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 16:05

    Suppose we have a dictionary like this

    Dictionary dict = new Dictionary();
    dict.Add(1, "Mohan");
    dict.Add(2, "Kishor");
    dict.Add(3, "Pankaj");
    dict.Add(4, "Jeetu");
    

    We can initialize it as follow.

    Dictionary dict = new Dictionary  
    {
        { 1, "Mohan" },
        { 2, "Kishor" },
        { 3, "Pankaj" },
        { 4, "Jeetu" }
    };
    

提交回复
热议问题