TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# List Remove Methods

These C# examples use the Remove method on the List type. They require System.Collections.Generic.

Remove eliminates elements from List.

The target elements may be found at certain indexes—they may have certain values. They may instead match a condition we specify. The Remove method is effective for this.

Example. To start, we show how to remove (erase) elements from your List. You can do this based on the element value you want to remove (with Remove), or based on the index (with RemoveAt). This is not equivalent to assigning an element to null.

RemoveAt

Next: This example shows Remove and RemoveAt. It removes the element with the value "bulldog", which erases the fourth element.

Then: It removes the element with index 1, which is the second dog, "otterhound".

C# program that uses Remove method

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
	List<string> dogs = new List<string>();
	dogs.Add("maltese");    // Contains maltese
	dogs.Add("otterhound"); // maltese, otterhound
	dogs.Add("rottweiler"); // maltese, otterhound, rottweiler
	dogs.Add("bulldog");    // ... rottweiler, bulldog
	dogs.Add("whippet");    // .... rottweiler, bulldog, whippet

	dogs.Remove("bulldog"); // Remove bulldog

	foreach (string dog in dogs)
	{
	    Console.WriteLine(dog);
	}
	// Contains: maltese, otterhound, rottweiler, whippet

	dogs.RemoveAt(1); // Remove second dog

	foreach (string dog in dogs)
	{
	    Console.WriteLine(dog);
	}
	// Contains: maltese, rottweiler, whippet
    }
}

Output

maltese
otterhound
rottweiler
whippet

maltese
rottweiler
whippet

Discussion. Methods on List that remove certain elements require linear time. Therefore, it could be faster to assign null to elements you want to erase them, rather than removing them. But this could complicate your code.

Note: RemoveAt is faster than Remove. It doesn't need to iterate through the number of elements equal to index.

Also, you can use RemoveAll to remove all elements in the List that match a certain predicate expression. It is often easiest to use the lambda expression syntax with the RemoveAll method, which can reduce line count.

RemoveAll

RemoveRange. Here we see the RemoveRange method, which can remove elements in a certain series of indexes. One useful way to use this method is to remove the first or last several elements at once.

Next: We remove all elements except the last two. The code is robust because it uses Math.Max to avoid negative parameters.

Math.Max

C# program that uses RemoveRange method

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
	List<int> list = new List<int>();
	list.Add(1);
	list.Add(2);
	list.Add(3);
	list.Add(4);
	list.Add(5);

	// Remove all except last 2
	int remove = Math.Max(0, list.Count - 2);
	list.RemoveRange(0, remove);

	foreach (int i in list)
	{
	    Console.Write(i);
	}
    }
}

Output

45

Next, another useful way to call RemoveRange is to remove the first N elements in your List. We also use Math.Min here to avoid arguments that are too large and would raise an exception.

Math.Min

C# program that uses RemoveRange on first elements

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
	List<int> list = new List<int>();
	list.Add(1);
	list.Add(2);
	list.Add(3);
	list.Add(4);
	list.Add(5);

	// Remove first 2 elements
	int remove = Math.Min(list.Count, 2);
	list.RemoveRange(0, remove);

	foreach (int i in list)
	{
	    Console.Write(i);
	}
    }
}

Output

345

 

Summary. We saw several useful examples of using the List type's Remove, RemoveAt, RemoveAll and RemoveRange methods. We found out how to remove the first N and last N elements, and also reviewed the algorithmic complexity of the methods.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf