Home >Backend Development >C#.Net Tutorial >How to iterate over a C# list?
Declare a list and add elements -
var products = new List < string > (); // adding elements products.Add("Belts"); products.Add("T-Shirt"); products.Add("Trousers");
Iterate using a loop-
foreach(var p in products) { Console.WriteLine(p); }
Let’s see it in full Example-
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); } } }
The above is the detailed content of How to iterate over a C# list?. For more information, please follow other related articles on the PHP Chinese website!