集合初始化器语法之间的区别

Difference Between Collection Initializer Syntaxes

MSDN 表示:

By using a collection initializer you do not have to specify multiple calls to the Add method of the class in your source code; the compiler adds the calls.

他们还给出了这个例子,使用带有方括号的较新的集合初始化语法:

var numbers = new Dictionary<int, string> { 
    [7] = "seven", 
    [9] = "nine", 
    [13] = "thirteen" 
};

然而,当检查生成的 IL 代码时,似乎这段代码根本不会导致对 Add 方法的任何调用,而是对一个 set_item 的调用,如下所示:

IL_0007: ldstr        "seven"
IL_000c: callvirt     instance void class [mscorlib]System.Collections.Generic.Dictionary`2<int32, string>::set_Item(!0/*int32*/, !1/*string*/)

相比之下,带有大括号的“旧”语法给出了以下内容:

// C# code:
var numbers2 = new Dictionary<Int32, String>
{
    {7, "seven"},
    {9, "nine"},
    {13, "thirteen"}
};

// IL code snippet:
// ----------
// IL_0033: ldstr        "seven"
// IL_0038: callvirt     instance void class [mscorlib]System.Collections.Generic.Dictionary`2<int32, string>::Add(!0/*int32*/, !1/*string*/)

... 如您所见,调用 Add 是预期的结果。 (只能假设上面提到的MSDN上的文字还没有更新。)

到目前为止,我发现了一种情况,这种差异确实很重要,那就是古怪的 System.Collections.Specialized.NameValueCollection。这允许一个键指向多个值。初始化可以通过两种方式完成:

const String key = "sameKey";
const String value1 = "value1";
const String value2 = "value2";

var collection1 = new NameValueCollection
{
    {key, value1},
    {key, value2}
};

var collection2 = new NameValueCollection
{
    [key] = value1,
    [key] = value2
};

...但是因为只有前者实际调用的方式不同NameValueCollection::Add(string, string),所以看每个集合的内容结果都不一样;

collection1[key] = "value1,value2"

collection2[key] = "value2"

我意识到旧语法和 IEnumerable 接口之间存在联系,以及编译器如何通过命名约定等找到 Add 方法。我也意识到任何索引器类型都受制于新语法的好处,如之前 中所讨论的那样。

从您的角度来看,也许这些都是预期的功能,但我没有想到其中的含义,我很想知道更多。

所以,我想知道 MSDN 或其他地方是否有文档来源阐明了语法选择带来的这种行为差异。我还想知道您是否知道在初始化 NameValueCollection.

时这种选择可能会产生影响的任何其他示例

我想为了最终的澄清,您必须查看规范。 C# 6 规范未 'officially' 发布,但有 unofficial draft 可用。

这里有趣的是,尽管它位于编程指南中,但索引器语法不是集合初始化器,它是对象初始化器。来自 7.6.11.3 'Collection Initializers':

A collection initializer consists of a sequence of element initializers, enclosed by { and } tokens and separated by commas. Each element initializer specifies an element to be added to the collection object being initialized, and consists of a list of expressions enclosed by { and } tokens and separated by commas. ... The collection object to which a collection initializer is applied must be of a type that implements System.Collections.IEnumerable or a compile-time error occurs. For each specified element in order, the collection initializer invokes an Add method on the target object with the expression list of the element initializer as argument list

并且来自 7.6.11.2 'Object Intializers'

An object initializer consists of a sequence of member initializers, enclosed by { and } tokens and separated by commas. Each member_initializer designates a target for the initialization. An identifier must name an accessible field or property of the object being initialized, whereas an argument_list enclosed in square brackets must specify arguments for an accessible indexer on the object being initialized.

以此为例:

public class ItemWithIndexer
{
    private readonly Dictionary<string, string> _dictionary = 
        new Dictionary<string, string>();

    public string this[string index]
    {
        get { return _dictionary[index]; }
        set { _dictionary[index] = value; }
    }
}

请注意,此 class 满足应用集合初始值设定项的要求:它不实现 IEnumerable 或没有 Add 方法,因此任何以这种方式初始化的尝试都会导致编译时错误。这个针对索引器的对象初始值设定项将编译并工作,但是(参见 this fiddle):

var item = new ItemWithIndexer
{
    ["1"] = "value"
};