<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Removing elements from a Dictionary</title><link>http://blogs.msdn.com/b/kevinpilchbisson/archive/2008/10/27/removing-elements-from-a-dictionary.aspx</link><description>The topic of removing elements from a dictionary came up recently on an internal mailing list.&amp;#160; Someone was iterating through all the elements of a collection and wanting to remove some of them. The problem with that is that modifying the collection</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>ForEach</title><link>http://blogs.msdn.com/b/kevinpilchbisson/archive/2008/10/27/removing-elements-from-a-dictionary.aspx#9387916</link><pubDate>Sun, 01 Feb 2009 10:42:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9387916</guid><dc:creator>Kirill Osenkov</dc:creator><description>&lt;p&gt;In my recent post about coding styles one particular thing provoked the majority of feedback and discussions:&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9387916" width="1" height="1"&gt;</description></item><item><title>re: Removing elements from a Dictionary</title><link>http://blogs.msdn.com/b/kevinpilchbisson/archive/2008/10/27/removing-elements-from-a-dictionary.aspx#9025593</link><pubDate>Fri, 31 Oct 2008 03:22:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9025593</guid><dc:creator>Pilchie (Kevin Pilch-Bisson)</dc:creator><description>&lt;p&gt;Uh-oh!&lt;/p&gt;
&lt;p&gt;You caught me making perf comments based on pure reasoning instead of empirical evidence.&lt;/p&gt;
&lt;p&gt;I didn't actually measure. &amp;nbsp;I'd love to hear about the results of your measurements though...&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9025593" width="1" height="1"&gt;</description></item><item><title>re: Removing elements from a Dictionary</title><link>http://blogs.msdn.com/b/kevinpilchbisson/archive/2008/10/27/removing-elements-from-a-dictionary.aspx#9025521</link><pubDate>Fri, 31 Oct 2008 02:39:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9025521</guid><dc:creator>Rik Hemsley</dc:creator><description>&lt;p&gt;Kevin, did you measure your ToArray() solution vs. my solution using a copy of the dictionary? I haven't profiled for memory usage, but a rather unscientific test using System.Diagnostics.Stopwatch shows the dictionary copy solution is clearly faster. I'd be interested to see what happens with RAM, but I'll have to wait until tomorrow to use a proper profiler.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9025521" width="1" height="1"&gt;</description></item><item><title>re: Removing elements from a Dictionary</title><link>http://blogs.msdn.com/b/kevinpilchbisson/archive/2008/10/27/removing-elements-from-a-dictionary.aspx#9024497</link><pubDate>Thu, 30 Oct 2008 17:14:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9024497</guid><dc:creator>Pilchie (Kevin Pilch-Bisson)</dc:creator><description>&lt;p&gt;Rik, Your solution also works, but the price you pay is the space and time overhead of creating a completly new dictionary that contains all of the original elements. &amp;nbsp;In a large collection, you may not want to pay that price, assuming that the average case is that you're only actually removing half of them. &amp;nbsp;You also don't need the second collection to actually be a dictionary, since you aren't making any use of the lookup functionality; you're just enumerating the contents.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9024497" width="1" height="1"&gt;</description></item><item><title>re: Removing elements from a Dictionary</title><link>http://blogs.msdn.com/b/kevinpilchbisson/archive/2008/10/27/removing-elements-from-a-dictionary.aspx#9024493</link><pubDate>Thu, 30 Oct 2008 17:13:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9024493</guid><dc:creator>Pilchie (Kevin Pilch-Bisson)</dc:creator><description>&lt;p&gt;Kirill, that _works_, but it loses the ability to do the delayed execution that Linq gets you. &amp;nbsp;It also means that you can't chain multiple &amp;quot;ForEach&amp;quot; calls together to do different things.&lt;/p&gt;
&lt;p&gt;Without delayed execution, you can't &amp;quot;ForEach/Apply&amp;quot; on an infinite collection, and not having the ability to chain means that you have lump all of your side-effects together.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9024493" width="1" height="1"&gt;</description></item><item><title>re: Removing elements from a Dictionary</title><link>http://blogs.msdn.com/b/kevinpilchbisson/archive/2008/10/27/removing-elements-from-a-dictionary.aspx#9024097</link><pubDate>Thu, 30 Oct 2008 12:34:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9024097</guid><dc:creator>Rik Hemsley</dc:creator><description>&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static Dictionary&amp;lt;Tkey, Tvalue&amp;gt; DeleteWhere&amp;lt;Tkey, Tvalue&amp;gt;(this Dictionary&amp;lt;Tkey, Tvalue&amp;gt; collection, Func&amp;lt;Tkey, Tvalue, bool&amp;gt; shouldDelete)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foreach (var keyValuePair in new Dictionary&amp;lt;Tkey, Tvalue&amp;gt;(collection))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (shouldDelete(keyValuePair.Key, keyValuePair.Value))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;collection.Remove(keyValuePair.Key);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return collection;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9024097" width="1" height="1"&gt;</description></item><item><title>re: Removing elements from a Dictionary</title><link>http://blogs.msdn.com/b/kevinpilchbisson/archive/2008/10/27/removing-elements-from-a-dictionary.aspx#9023946</link><pubDate>Thu, 30 Oct 2008 11:10:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9023946</guid><dc:creator>Kirill Osenkov - MSFT</dc:creator><description>&lt;p&gt;And how about:&lt;/p&gt;
&lt;p&gt;using System;&lt;/p&gt;
&lt;p&gt;using System.Collections.Generic;&lt;/p&gt;
&lt;p&gt;using System.Linq;&lt;/p&gt;
&lt;p&gt;static class Program&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;static void Main(string[] args)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dictionary&amp;lt;string, int&amp;gt; d = new Dictionary&amp;lt;string, int&amp;gt;()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&amp;quot;one&amp;quot;, 1},&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&amp;quot;two&amp;quot;, 2}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;};&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;d.Where(pair =&amp;gt; pair.Value &amp;lt; 2).ToArray().ForEach(pair =&amp;gt; d.Remove(pair.Key));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;static void ForEach&amp;lt;T&amp;gt;(this IEnumerable&amp;lt;T&amp;gt; list, Action&amp;lt;T&amp;gt; action)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foreach (var item in list)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;action(item);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9023946" width="1" height="1"&gt;</description></item><item><title>re: Removing elements from a Dictionary</title><link>http://blogs.msdn.com/b/kevinpilchbisson/archive/2008/10/27/removing-elements-from-a-dictionary.aspx#9020320</link><pubDate>Tue, 28 Oct 2008 17:58:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9020320</guid><dc:creator>Pilchie (Kevin Pilch-Bisson)</dc:creator><description>&lt;p&gt;Yes, realize works there, and I do like the point about intent over mechanism.&lt;/p&gt;
&lt;p&gt;I chose the no arg &amp;quot;Apply&amp;quot; in deference to Jon Pryor (&lt;a rel="nofollow" target="_new" href="http://www.jprl.com/Blog/"&gt;http://www.jprl.com/Blog/&lt;/a&gt;), who I've discussed the &amp;quot;Apply&amp;quot; method with a bunch of times.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9020320" width="1" height="1"&gt;</description></item><item><title>re: Removing elements from a Dictionary</title><link>http://blogs.msdn.com/b/kevinpilchbisson/archive/2008/10/27/removing-elements-from-a-dictionary.aspx#9020273</link><pubDate>Tue, 28 Oct 2008 17:30:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9020273</guid><dc:creator>Jay Bazuzi</dc:creator><description>&lt;p&gt;You talked about naming the no-action 'Apply()' overload 'Realize()'. &amp;nbsp;I think a different name is a good idea, because it really does something quite different than the other 'Apply()'&lt;/p&gt;
&lt;p&gt;What if you write it like this:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public static IEnumerable&amp;lt;T&amp;gt; Apply&amp;lt;T&amp;gt;(this IEnumerable&amp;lt;T&amp;gt; source)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return source.ToList();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;And then replace your 'ToArray()' call with &lt;/p&gt;
&lt;p&gt;'Realize()'?&lt;/p&gt;
&lt;p&gt;I think this makes your code a little closer to intent, a little further from mechanism.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9020273" width="1" height="1"&gt;</description></item></channel></rss>