TheDeveloperBlog.com

Home | Contact Us

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

C# RemoveAll List Method

This C# example program uses the RemoveAll method on the List type.

RemoveAll filters and removes elements.

It can be used with a lambda expression. This reduces the size of your code and improves its clarity. The List RemoveAll method accepts a Predicate expression for this purpose.

Predicate

Example. First, this example removes all elements with values of 2. The lambda expression, with the => syntax, matches elements with value of 2. You could use another expression, such as item => item != 2, to remove elements not equal to 2.

C# program that uses RemoveAll 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(2);
	list.Add(4);
	list.Add(5);

	// Remove all list items with value of 2.
	// The lambda expression is the Predicate.
	list.RemoveAll(item => item == 2);

	// Display results.
	foreach (int i in list)
	{
	    Console.WriteLine(i);
	}
    }
}

Output

1
4
5

A lambda expression is a compact syntax form of an anonymous delegate method. You can use the delegate keyword with an optional parameter list and a method body instead of a lambda expression.

Info: Usually, lambda expressions are best kept simple to preserve code clarity. For complex methods, using a full declaration is simpler.

When reading the lambda expression operator => in the C# language, it helps to think of it as "goes to." So you can say that the RemoveAll invocation above "removes all elements where the value goes to 2."

Discussion. There are several other methods in the Remove family on List, including the Remove and RemoveAt methods. If you want to remove an isolated element, use those methods instead. The best removal method is determined by the context.

Tip: This site has more information on Remove, and much more information on Lists in general.

List Remove

Summary. You can use the RemoveAll method on the List type in the C# language. We noted how to use a lambda expression as the Predicate object to this method, and how you can read lambda expressions.

Thus: Using the RemoveAll method instead of a complex loop with multiple tests and copies can make code more readable.


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