EN
C# / .NET - clear List
0 points
In this article, we would like to show you how to clear List in C#.
Quick solution:
xxxxxxxxxx
1
List<string> myList = new List<string> { "A", "B", "C" };
2
3
myList.Clear();
In this example, we use Clear()
method to remove all items from myList
.
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
Console.WriteLine("Before:");
11
foreach (string item in myList)
12
Console.WriteLine(item);
13
14
myList.Clear(); // clear List
15
16
Console.WriteLine("\nAfter:");
17
foreach (string item in myList)
18
Console.WriteLine(item);
19
}
20
}
Output:
xxxxxxxxxx
1
Before:
2
A
3
B
4
C
5
6
After: