EN
C# / .NET - remove items from List
0 points
In this article, we would like to show you how to remove items from List in C#.
Quick solution:
xxxxxxxxxx
1
List<string> myList = new List<string> { "A", "B", "C", "A" };
2
3
myList.Remove("A");
4
5
myList.ForEach(x => Console.WriteLine(x)); // Output: B,C,A
or:
xxxxxxxxxx
1
List<string> myList = new List<string> { "A", "B", "C", "A" };
2
3
myList.RemoveAt(0);
4
5
myList.ForEach(x => Console.WriteLine(x)); // Output: B,C,A
In this example, we use Remove()
method to remove item from myList
by value.
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", "A" };
9
10
myList.Remove("A");
11
12
foreach (string item in myList)
13
Console.WriteLine(item);
14
}
15
}
Output:
xxxxxxxxxx
1
B
2
C
3
A
In this example, we use RemoveAt()
method to remove item at specific index 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", "A" };
9
10
myList.RemoveAt(0);
11
12
foreach (string item in myList)
13
Console.WriteLine(item);
14
}
15
}
Output:
xxxxxxxxxx
1
B
2
C
3
A