TheDeveloperBlog.com

Home | Contact Us

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

<< Back to C-SHARP

C# RemoveAll: Use Lambda to Delete From List

Use the RemoveAll method on the List type with a lambda expression to remove matching elements.
RemoveAll. This filters and removes elements. It can be used with a lambda expression. This reduces the size of your code and improves its clarity.ListLambda
Some notes. The List RemoveAll method accepts a Predicate expression for this purpose. We can read it like a sentence: remove all elements matching this condition.Predicate
First example. This example code deletes all elements with values of 2. The lambda expression, with the => syntax, matches elements with value of 2.

Lambda: A lambda expression is a compact syntax form of an anonymous delegate method.

Tip: 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.

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
Return value. When called, RemoveAll will invoke the Predicate method passed to it. After this is done, it will return an int equal to the number of elements it removed from the List.

Count: The return int is the count of items removed—so if 2 items are removed, the return value is 2.

Nothing removed: If no elements were removed, RemoveAll will return 0. We can check for 0 to see if the list remained unchanged.

Join: This example displays the List contents by calling the string.Join method, which supports a List argument.

Join
C# program that uses RemoveAll return value using System; using System.Collections.Generic; class Program { static bool IsRemovable(string item) { return item.StartsWith("b"); } static bool IsRemovableNothingMatches(string item) { return item == "x"; } static void Main() { var items = new List<string> { "bird", "frog", "bat" }; Console.WriteLine("ITEMS: {0}", string.Join(",", items)); // Remove 2 items, the result value is 2. var result = items.RemoveAll(IsRemovable); Console.WriteLine("COUNT OF ITEM REMOVED: {0}", result); Console.WriteLine("ITEMS: {0}", string.Join(",", items)); // Nothing is removed with this call. var result2 = items.RemoveAll(IsRemovableNothingMatches); // The result is 0. Console.WriteLine("COUNT, NOTHING REMOVED: {0}", result2); } } Output ITEMS: bird,frog,bat COUNT OF ITEM REMOVED: 2 ITEMS: frog COUNT, NOTHING REMOVED: 0
Notes, lambda. When reading the lambda expression, 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."
A discussion. There are other removal methods on List like the Remove and RemoveAt methods. If you want to remove an isolated element, use those methods instead.List Remove

Tip: The simplest method—the one that is the easiest to read—is usually the best choice.

A summary. You can use the RemoveAll method on the List. We noted how to use a lambda expression as the Predicate object to this method, and how you can read lambda expressions.
Using the RemoveAll method instead of a complex loop with multiple tests and copies can make code more readable. But be careful to keep your lambda expressions as short as possible.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


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