What benefits does dictionary initializers add over collection initializers?

前端 未结 3 934
深忆病人
深忆病人 2020-12-03 16:43

In a recent past there has been a lot of talk about whats new in C# 6.0
One of the most talked about feature is using Dictionary initializers in C# 6.0

3条回答
  •  感动是毒
    2020-12-03 17:13

    New is creating a dictionary this way

    Dictionary myDict = new Dictionary() {
        [1] = "Pankaj",
        [2] = "Pankaj",
        [3] = "Pankaj"
    };
    

    with the style of =

    Obsolete: string indexed member syntax (as stated in the comments)

    Dictionary myDict = new Dictionary() {
            $1 = "Pankaj",
            $2 = "Pankaj",
            $3 = "Pankaj"
        };
    

    Taken from A C# 6.0 Language Preview

    To understand the $ operator, take a look at the AreEqual function call. Notice the Dictionary member invocation of “$Boolean” on the builtInDataTypes variable—even though there’s no “Boolean” member on Dictionary. Such an explicit member isn’t required because the $ operator invokes the indexed member on the dictionary, the equivalent of calling buildInDataTypes["Boolean"].

提交回复
热议问题