有一个我不知道该术语的C#功能,因此无法找到有关它的文档。
允许您在初始化期间将元素添加到集合中的“类似AddRange”语法的名称是什么?我专门指的是下面代码中记录的情况,其中我们没有调用构造函数(由于该属性没有设置器而无法调用),而是似乎只是在集合上调用AddRange。
using System;
using System.Collections.Generic;
public class Program
{
public static void Main(string[] args)
{
TestClass a = new TestClass()
{
// What is the name for this feature??
List =
{
0,
10,
20,
30
}
};
Console.WriteLine(a.List.Count);
}
public class TestClass
{
private List<int> _list = new List<int>();
public List<int> List { get { return _list; } }
}
}
另外,我是否正确理解此语法的语义?它只是允许我们对集合进行简写的AddRange / multiple-Add语法,对吗?
.NET Fiddle在这里,如果有人想运行示例代码:https : //dotnetfiddle.net/i01HYv
它是C#3.0中引入的Object Initializer或Collection Initializer语法
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句