Click here to Skip to main content
Click here to Skip to main content

Tagged as

Implementing Dictionary.RemoveAll

, 14 May 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Implementing Dictionary.RemoveAll as an extension method


Before I start, this is my first post - hope it is useful to at least a few readers. I've tried to start off with a simple topic - please comment on the general style and usability. I'll definitely do some more complicated posts and articles later on.  

.NET implements RemoveAll for any IList<T> but does not do so for IDictionary<K, V>. IList.RemoveAll removes any element that matches the predicate provided, IDictionary.RemoveAll does the same. 

Using the code

Copy and paste the extension method into any static class in your project (I always have at least one for extension methods). 

The code for the extension method:  

public static void RemoveAll<K, V>(this IDictionary<K, V> dict, Func<K, V, bool> match)
    foreach (var key in dict.Keys.ToArray()
            .Where(key => match(key, dict[key])))

An example of using it: 

var dictionary = new Dictionary<string, int>();
dictionary["A"] = 0;
dictionary["B"] = 1;
dictionary["C"] = 2;
dictionary["D"] = 3;
//The next line will remove the items with keys "C" and "D"
//since they both have values that are larger than 1
dictionary.RemoveAll((key, value) => value > 1); 

Points of Interest

A note on the implementation of the extension method:

Since you cannot remove items from an enumeration (the right side of a foreach statement is always an enumeration) during foreach, LINQ's .ToArray() is used to build an array of the items that should be removed and then the removals are done afterwards. This provides an easy-to-use syntax that is readable and does all the caching in one line. Of course, if you wanted to use this in performance critical situations or for high volumes of rows there are much better ways. 


  • 2012/11/16 - Posted.
  • 2012/11/17 - Updated with performance enhancement recommended by George Swan. 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Michiel du Toit
Software Developer Coderon Technologies
South Africa South Africa
Michiel du Toit is a software developer based in Bloemfontein, South Africa focusing on development using C# and SQL Server (both WinForms and ASP.NET).

Comments and Discussions

Questionoriginal? Pinmembercharles henington15-May-13 0:47 
QuestionFaster Alternative? PinmemberGeorge Swan16-Nov-12 8:47 
AnswerRe: Faster Alternative? PinmemberMichiel du Toit16-Nov-12 12:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150302.1 | Last Updated 14 May 2013
Article Copyright 2012 by Michiel du Toit
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid