StriBog Asked:2020-10-03 23:57:11 +0800 CST2020-10-03 23:57:11 +0800 CST 2020-10-03 23:57:11 +0800 CST C# ImmutableList 怎么用呢? 772 一开始我绊倒了,我无法初始化列表,显然是由于列表是不可变的如何使用它?如何为它分配一个数组? ImmutableList<int> firstList = new ImmutableList<int>(); c# 3 个回答 Voted Best Answer HasmikGaryaka 2020-10-04T00:47:34+08:002020-10-04T00:47:34+08:00 下面是一个创建示例 var builder = ImmutableList.CreateBuilder<string>(); builder.Add("1"); // Adds item to the existing object ImmutableList<string> list = builder.ToImmutable(); ImmutableList<string> list2 = list.Add("2"); // Creates a new object with 2 items 取自这里 VladD 2020-10-04T02:58:03+08:002020-10-04T02:58:03+08:00 另外的选择: int[] data = {1, 2, 3}; ImmutableList<int> firstList = ImmutableList.CreateRange(data); Vadim Sakhanenko 2020-02-01T04:53:44+08:002020-02-01T04:53:44+08:00 或像这样: int[] data = {1, 2, 3}; var firstList = data.ToImmutableList();
下面是一个创建示例
取自这里
另外的选择:
或像这样: