How to add items to a list in C#?

CsharpProgrammingServer Side Programming

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);
      }
   }
}
raja
Published on 14-Aug-2018 07:29:43
Advertisements