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:
xxxxxxxxxx
1
List<string> myList = new List<string>();
2
3
myList.Add("A");
In this example, we add three elements to myList
using Add()
method.
xxxxxxxxxx
1
using System;
2
using System.Collections.Generic;
3
4
public class Program
5
{
6
public static void Main()
7
{
8
List<string> myList = new List<string>();
9
10
myList.Add("A");
11
myList.Add("B");
12
myList.Add("C");
13
14
foreach (string item in myList)
15
Console.WriteLine(item);
16
}
17
}
Output:
xxxxxxxxxx
1
A
2
B
3
C
In this example, we present how to add items while creating List.
xxxxxxxxxx
1
using System;
2
using System.Collections.Generic;
3
4
public class Program
5
{
6
public static void Main()
7
{
8
List<string> myList = new List<string> { "A", "B", "C" };
9
10
foreach (string item in myList)
11
Console.WriteLine(item);
12
}
13
}
Output:
xxxxxxxxxx
1
A
2
B
3
C