C# | Removing the specified element from the List
List.Remove(T) Method is used to remove the first occurrence of a specific object from the List.
Properties of List:
- It is different from the arrays. A list can be resized dynamically but arrays cannot.
- List class can accept null as a valid value for reference types and it also allows duplicate elements.
- If the Count becomes equals to Capacity then the capacity of the List increases automatically by reallocating the internal array. The existing elements will be copied to the new array before the addition of the new element.
Syntax:
public bool Remove (T item);
Parameter:
item: Specified object which is to be remove from the List.
Return Type: This method returns True if item is successfully removed. Otherwise it returns False.
Note: This method returns False if item was not found in the List.
Below programs illustrate how to remove the specified element from the List:
Example 1:
// C# program to remove the specified // element from the List<T> using System; using System.Collections.Generic; class Geeks { // Main Method public static void Main(String[] args) { // Creating a List of integers List<int> firstlist = new List<int>(); // adding elements in firstlist firstlist.Add(1); firstlist.Add(2); firstlist.Add(3); firstlist.Add(4); // Displaying elements of firstlist // by using foreach loop Console.WriteLine("Before Removing"); foreach(int element in firstlist) { Console.WriteLine(element); } // Removing 2 from the firstlist & Displaying // the remaining firstlist elements Console.WriteLine("After Removing"); firstlist.Remove(2); foreach(int element in firstlist) { Console.WriteLine(element); } } } |
Output:
Before Removing 1 2 3 4 After Removing 1 3 4
Example 2:
// C# program to remove the specified // element from the List<T> using System; using System.Collections.Generic; class Geeks { // Main Method public static void Main(String[] args) { // Creating a List of integers List<int> firstlist = new List<int>(); // adding elements in firstlist firstlist.Add(1); firstlist.Add(2); firstlist.Add(3); firstlist.Add(4); // Adding some duplicate // elements in firstlist firstlist.Add(2); firstlist.Add(4); // Displaying elements of firstlist // by using foreach loop Console.WriteLine("Before Removing"); foreach(int element in firstlist) { Console.WriteLine(element); } // Removing first occurrence of 2 // from the firstlist & Displaying // the remaining firstlist elements Console.WriteLine("After Removing"); firstlist.Remove(2); foreach(int element in firstlist) { Console.WriteLine(element); } } } |
Output:
Before Removing 1 2 3 4 2 4 After Removing 1 3 4 2 4
Reference:
Recommended Posts:
- C# | Removing all the elements from the List
- C# | Removing a range of elements from the List
- C# | Adding an element to the List
- C# | How to check whether a List contains a specified element
- C# | How to remove the element from the specified index of the List
- C# | Gets or Sets the element at the specified index in the List
- C# | How to perform a specified action on each element of the List
- C# | How to get the last occurrence of the element in the List that match the specified conditions
- C# | Check if every List element matches the predicate conditions
- Removing the object from the top of the Stack in C#
- C# | Removing all entries from HybridDictionary
- C# | Removing the node at the end of LinkedList<T>
- C# | Removing all nodes from LinkedList<T>
- C# | Removing the specified key entry from HybridDictionary
- C# | Removing all entries from the StringDictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


