我不知道如何搜索这个问题; 如果它是多余的,请原谅我.
所以,我有一些像这样的代码:
textBox1.InputScope = new InputScope { Names = { _Scope } };
Run Code Online (Sandbox Code Playgroud)
Names属性的类型为IList
我的代码是在列表中添加项目还是创建新列表?
额外的花括号是做什么的?
Jon*_*eet 15
这是一个集合初始值设定项,但是没有创建新集合 - 它只是添加到现有集合中.它用作初始值 a的部分成员初始化一个内对象的初始值设定.它相当于:
InputScope tmp = new InputScope();
tmp.Names.Add(_Scope);
textBox1.InputScope = tmp;
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅C#4规范的7.6.10.3节.
new InputScope { // indicates an object-initializer for InputScope using
// the default constructor
Names = { // indicates an in-place usage of a collection-initializer
_Scope // adds _Scope to Names
} // ends the collection-initializer
}; // ends the object-initializer
Run Code Online (Sandbox Code Playgroud)
即
var tmp = new InputScope();
tmp.Names.Add(_Scope);
textBox1.InputScope = tmp;
Run Code Online (Sandbox Code Playgroud)