Class sam
{
public void m1()
{
List<int> A = new List<int>() {1,2,3};
List<int> B = new List<int>() {4,5,6};
for (int i = 0; i < A.count; i++)
{
c.add(m2(A[i], B[i]));
}
}
public int M2(int a, int b)
{
return a + b;
}
}
在这个程序中,我从两个列表中检索并传递参数。我喜欢在linQ中调用方法m2
答案 0 :(得分:4)
从问题中不清楚,但也许你的意思是:
var C = A.Zip(B, (a, b) => a + b)
.ToList();
如果您想将M2
方法添加到其中,可以执行以下操作:
// C# 4
var C = A.Zip(B, M2);
// C# 3 (not likely since Zip was introduced in .NET 4)
var C = A.Zip(B, (a, b) => M2(a, b));
var C = A.Zip<int, int, int>(B, M2);