gpt4 book ai didi

c# - "AddRange"集合初始值设定项的名称?

转载 作者:行者123 更新时间:2023-11-30 23:05:18 25 4
gpt4 key购买 nike

<分区>

有一个 C# 功能,我不知道它的术语,因此无法找到关于它的文档。

允许您在初始化期间向集合中添加元素的“类 AddRange”语法的名称是什么?我特别指的是下面代码中记录的案例,我们没有调用构造函数(并且由于属性没有 setter 而无法调用),而是似乎只是在集合上调用 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

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