How to Remove Objects from the List with RemoveAll in C#?
Below is a sample code snippet demonstrating How to Remove Objects from the List with RemoveAll in C#?
How to Remove Objects from the List with RemoveAll in C#?
C#
x
57
57
1
using System;
2
3
using System.Collections.Generic;
4
5
using System.Data;
6
7
using System.Linq;
8
9
namespace AbundantCode
10
11
{
12
13
internal class Program
14
15
{
16
17
//How to Remove Objects from the List with RemoveAll in C# ?
18
19
private static void Main(string[] args)
20
21
{
22
23
List<CodeSnippet> snippets = new List<CodeSnippet>()
24
25
{
26
27
new CodeSnippet { Code = "1" , Name = "Abundantcode1"},
28
29
new CodeSnippet { Code = "2" , Name = "Abundantcode2"}
30
31
};
32
33
// Removes the item with "Code=1" from the list
34
35
snippets.RemoveAll(a => a.Code == "1");
36
37
foreach (var item in snippets)
38
39
Console.WriteLine(item.Name);
40
41
Console.ReadLine();
42
43
}
44
45
}
46
47
public class CodeSnippet
48
49
{
50
51
public string Name { get; set; }
52
53
public string Code { get; set; }
54
55
}
56
57
}
Leave Your Comment