How to add items to a list in C#?


Firstly, declare a list −

var teams = new List<string>();

To add items to a C# list, use the Add() method −

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

You can try to run the following code to add items to a list in C# −

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

Updated on: 21-Jun-2020

631 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements