Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Remove() - Method


Remove() Method


Remove() method is used to remove an element from the List by its name/value or index.


Let us say, we have a List that contains three names, Mohan, Kriti and Salim. And we want to remove Kriti from the List.


It can be done with the remove() Method


Example :



using System.Collections.Generic;
    
public class MyApplication
{
    public static void Main(string[] args)
    {
        var x = new List<string>(){"Mohan", "Kriti", "Salim"};
        
        x.Remove("Kriti");
        
        foreach (var data in x)  
        {  
            System.Console.WriteLine(data);  
        }
    }    
}


Output :



  Mohan
  Salim

So, in the above code we have created a List and initialised to the variable x.


var x = new List<string>(){"Mohan", "Kriti", "Salim"};

Below is how the values are positioned in the List,

C_Sharp

Next, we have used the Remove() function that searches for the name Kriti and removes it from the List.


x.Remove("Kriti");
C_Sharp

Output :



  Mohan
  Salim