C# 将值附加到列表中

C# 将值附加到列表中,c#,.net,collections,C#,.net,Collections,有没有办法在不使用循环的情况下将数字列表添加到列表中 我的设想: List<int> test = CallAMethod(); // It will return a List with values 1,2 test = CallAMethod(); // It will return a List with values 6,8 List test=CallAMethod();//它将返回一个值为1,2的列表 test=CallAMethod()

有没有办法在不使用循环的情况下将数字列表添加到
列表中

我的设想:

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

test = CallAMethod();             // It will return a List with values 6,8
List test=CallAMethod();//它将返回一个值为1,2的列表
test=CallAMethod();//它将返回一个值为6,8的列表
但现在第二组值将取代第一组值。有没有办法在不使用for循环的情况下将值附加到列表中?

您需要执行以下操作:

lst.AddRange(callmethod());
或者,C#3.0只需使用 ,

e、 g

您需要执行以下操作:

lst.AddRange(callmethod());
或者,C#3.0只需使用 ,

e、 g


这应该可以做到:

test.AddRange(CallAMethod());

这应该可以做到:

test.AddRange(CallAMethod());

将列表作为CallAMethod的参数,并向其中添加项目,而不是每次返回一个新列表,怎么样

List<int> test = new List<int>();
CallAMethod(test); // add 1,2
CallAMethod(test); // add 6,8
List test=newlist();
CallAMethod(测试);//加1,2
CallAMethod(测试);//加6,8
然后你定义CallAMethod为

void CallAMethod(List<int> list) {
    list.Add( /* your values here */ );
}
void CallAMethod(列表){
列表。在此处添加(/*您的值*/);
}

将列表作为CallAMethod的参数,并向其中添加项目,而不是每次都返回一个新列表,怎么样

List<int> test = new List<int>();
CallAMethod(test); // add 1,2
CallAMethod(test); // add 6,8
List test=newlist();
CallAMethod(测试);//加1,2
CallAMethod(测试);//加6,8
然后你定义CallAMethod为

void CallAMethod(List<int> list) {
    list.Add( /* your values here */ );
}
void CallAMethod(列表){
列表。在此处添加(/*您的值*/);
}