首先,设置列表-
List<int> myList = new List<int> () { 5, 10, 7 };
现在,将变量的值设置为1,这将有助于我们乘以-
int prod = 1;
循环并获得产品-
foreach(int i in myList) { prod = prod*i; }
以下是代码-
using System; using System.Collections.Generic; public class Program { public static void Main() { List<int> myList = new List<int>() { 5, 10, 7 }; Console.WriteLine("List: "); foreach(int i in myList) { Console.WriteLine(i); } int prod = 1; foreach(int i in myList) { prod = prod*i; } Console.WriteLine("Product: {0}",prod); } }
输出结果
List: 5 10 7 Product: 350