Home  >  Article  >  Backend Development  >  How to add items to a list in C#?

How to add items to a list in C#?

王林
王林forward
2023-08-29 08:57:16941browse

如何在 C# 中向列表添加项目?

First, declare a list -

var teams = new List<string>();

To add an item to a C# list, use the Add() method -

teams.Add("US");
teams.Add("Canada");
teams.Add("India");
teams.Add("Australia");

You can try Run the following code to add an item to a C# list -

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(string[] args) {

      var teams = new List<string>();
      teams.Add("US");
      teams.Add("Canada");
      teams.Add("India");
      teams.Add("Australia");
   
      Console.WriteLine("Elements...");
      foreach (var countries in teams) {
         Console.WriteLine(countries);
      }
   }
}

The above is the detailed content of How to add items to a list in C#?. 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