有没有一种方法可以将2个元素中的元素求和到C#中的新列表中

时间:2020-04-23 19:09:45

标签: c# list

我有两个不同的相同计数列表,试图将它们汇总成一个新列表

 List<int> Calculate = new List<int>();
 List<int> new_Quantity= new List<int>();
 List<int> Qty= new List<int>();

,所以让new_Quantity假设其int值为[2,3,4] Qty的int值为[2,4,5]。所以我希望新列表能够计算出[4,7,9] 这是到目前为止使用的

For each(var i in new_Quantity)
{
   for each(var j in Qty)
   {
      calculate.add( i + j);
   }
}

但是没有得到我想要的精确计算,因为此输出为[4,7,9,4,7,9,4,7,9]; 那么我该如何解决呢?

1 个答案:

答案 0 :(得分:0)

列出3个列表:

static List<int> List1 = new List<int>();
static List<int> List2 = new List<int>();
static List<int> List3 = new List<int>();


void Sample()
    {
        List1.Add(2);
        List2.Add(3);

        for(int i=0;i<List1.Count();i++)
        {
            List3.Add(List1[i] + List2[i]);
        }
    }

因此您已在List3中汇总