gpt4 book ai didi

c# - 将值附加到列表

转载 作者:太空宇宙 更新时间:2023-11-03 20:34:28 27 4
gpt4 key购买 nike

有什么方法可以将数字列表添加到 List<int> 中吗?不使用循环?

我的场景:

List<int> test = CallAMethod();   // It will return a List with values 1,2

test = CallAMethod(); // It will return a List with values 6,8

但现在第二组值将取代第一组。有没有办法在没有 for 循环的情况下将值附加到列表?

最佳答案

List.AddRange Method

您需要执行以下操作:

lst.AddRange(callmethod());

或者,C# 3.0,只需使用 Concat ,

例如

lst.Concat(callmethod()); // and optionally .ToList()

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

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