如何遍历C#列表?
声明列表并添加元素-
var products = new List < string > (); //添加元素 products.Add("Belts"); products.Add("T-Shirt"); products.Add("Trousers");
使用循环来迭代-
foreach(var p in products) { Console.WriteLine(p); }
示例
让我们看完整的例子-
using System; using System.Collections.Generic; public class Demo { public static void Main(string[] args) { var products = new List < string > (); products.Add("Belts"); products.Add("T-Shirt"); products.Add("Trousers"); Console.WriteLine("Our list...."); foreach(var p in products) { Console.WriteLine(p); } } }