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

How to iterate over a C# list?

PHPz
Release: 2023-09-17 11:05:02
forward
680 people have browsed it

如何迭代 C# 列表?

Declare a list and add elements -

var products = new List < string > ();

// adding elements
products.Add("Belts");
products.Add("T-Shirt");
products.Add("Trousers");
Copy after login

Iterate using a loop-

foreach(var p in products) {
   Console.WriteLine(p);
}
Copy after login

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);
      }
   }
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template