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 -
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!