EN
C# / .NET - clear List
0
points
In this article, we would like to show you how to clear List in C#.
Quick solution:
List<string> myList = new List<string> { "A", "B", "C" };
myList.Clear();
Practical example
In this example, we use Clear() method to remove all items from myList.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<string> myList = new List<string> { "A", "B", "C" };
Console.WriteLine("Before:");
foreach (string item in myList)
Console.WriteLine(item);
myList.Clear(); // clear List
Console.WriteLine("\nAfter:");
foreach (string item in myList)
Console.WriteLine(item);
}
}
Output:
Before:
A
B
C
After: