How to remove elements from a list in C#

How to remove elements from a list in C#

 

List.Remove()


The Remove() method takes an item as a ​parameter and removes the first occurrence of that item from the list

 

using System.Collections.Generic;

class usingcsharp
{
    static void Main()
    {
      // Creating a list of strings
      List<string> item = new List<string>();

      // Adding items to the list
      item.Add("A");    
      item.Add("B");
      item.Add("C");   


      System.Console.WriteLine("Original List:");
      // Printing the list
      item.ForEach(System.Console.WriteLine); 

      // Calling the Remove() method

      item.Remove("B");

      System.Console.WriteLine();

      System.Console.WriteLine("After removing B:");
     
      item.ForEach(System.Console.WriteLine); 
    }
}

 

2021-10-03