gpt4 book ai didi

c# - 集合初始化语法之间的差异

转载 作者:太空狗 更新时间:2023-10-29 23:20:48 24 4
gpt4 key购买 nike

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 接口(interface)之间存在联系,以及编译器如何通过命名约定等找到 Add 方法。我还意识到任何索引器类型都受制于新语法的好处,如 this SO answer 中所述。之前。

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

因此,我想知道 MSDN 或其他地方是否有文档来源阐明了语法选择带来的这种行为差异。我还想知道您是否知道在初始化 NameValueCollection 时这种选择可能会产生影响的任何其他示例。

最佳答案

我想为了最终的澄清,您必须查看规范。 C# 6 规范并未“正式”发布,但有一个 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; }
}
}

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

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

关于c# - 集合初始化语法之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39102471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com