在 python 中,* 允许我将列表作为函数参数传递:
def add(a,b): return a+b
x = [1,2]
add(*x)
我可以使用数组在 C# 中复制此行为吗?
谢谢。
最佳答案
params关键字允许类似的东西
public int Add(params int[] numbers) {
int result = 0;
foreach (int i in numbers) {
result += i;
}
return result;
}
// to call:
int result = Add(1, 2, 3, 4);
// you can also use an array directly
int result = Add(new int[] { 1, 2, 3, 4});
关于C#,将数组作为函数参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2023528/