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




C# - REMOVE FROM LIST


How to remove an element from the List by its name/value?


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

How to remove an element from the List by its index/position?


Let us say, we have a List that contains three names, Mohan, Kriti and Salim. And we want to remove the element at index/position 2 from the List.


So, we can use RemoveAt() function to remove an element from the List.


Let us see the example with del keyword first.


Example :



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


Output :



  Mohan
  Kriti

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 removeAt() function that searches for the element at index/position 2 and removes it from the List.


x.RemoveAt(2);

And as we can see, there is Salim at index/position 2. So Salim is removed from the List.

C_Sharp

And we get the below output,

Output :



  Mohan
  Kriti

How to remove all the elements from the List?


The Clear() Method can be used to remove all the elements from the List.


Example :



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


Output :




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 Clear() function that removes all the elements from the List making the List empty.


And we get an empty List as output,