Home >Backend Development >C#.Net Tutorial >How to iterate over a C# list?

How to iterate over a C# list?

PHPz
PHPzforward
2023-09-17 11:05:02681browse

如何迭代 C# 列表?

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);
}

Example

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Reverse an array using C#Next article:Reverse an array using C#