gpt4 book ai didi

c# - 在列表中添加多个值

转载 作者:太空宇宙 更新时间:2023-11-03 19:39:41 25 4
gpt4 key购买 nike

是否可以将多个项目添加到列表中或将值列表添加到列表中。

这是我目前的伪代码:

List<string> myList = new List<string>();
myList.add("a","b","c","d","e","f","g");
myList.add("h","i","j","k","l","m","n");
myList.add("a1","a2","a3");

我的预期结果是:

[["a","b","c","d","e","f","g"], ["h","i","j","k","l","m","n"], ["a1","a2","a3"]]

任何建议/评论 TIA。

最佳答案

您要的是 List<List<string>> .可能有更好的结构来存储您的数据,但由于您没有提供任何上下文,您可以这样做:

var myList = new List<List<string>>();

然后像这样添加项目:

myList.Add(new List<string> { "a", "b", "c", "d", "e", "f", "g" });
myList.Add(new List<string> { "h", "i", "j", "k", "l", "m", "n" });
myList.Add(new List<string> { "a1", "a2", "a3" });

或者在一段代码中使用 collection initialiser :

var myList = new List<List<string>>
{
new List<string> { "a", "b", "c", "d", "e", "f", "g" },
new List<string> { "h", "i", "j", "k", "l", "m", "n" },
new List<string> { "a1", "a2", "a3" }
};

关于c# - 在列表中添加多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55465870/

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