EN
C# / .NET - add items to List
0
points
In this article, we would like to show you how to add items to List in C#.
Quick solution:
List<string> myList = new List<string>();
myList.Add("A");
Practical example
In this example, we add three elements to myList using Add() method.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<string> myList = new List<string>();
myList.Add("A");
myList.Add("B");
myList.Add("C");
foreach (string item in myList)
Console.WriteLine(item);
}
}
Output:
A
B
C
2. Add items on creation
In this example, we present how to add items while creating List.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<string> myList = new List<string> { "A", "B", "C" };
foreach (string item in myList)
Console.WriteLine(item);
}
}
Output:
A
B
C