<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.infosupport.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">Frank Bakker</title><subtitle type="html">Frank Bakker talks about software development</subtitle><id>http://blogs.infosupport.com/blogs/frankb/atom.aspx</id><link rel="alternate" type="text/html" href="http://blogs.infosupport.com/blogs/frankb/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.infosupport.com/blogs/frankb/atom.aspx" /><generator uri="http://communityserver.org" version="4.1.31106.3070">Community Server</generator><updated>2007-03-22T21:00:00Z</updated><entry><title>Takeaways from PDC09</title><link rel="alternate" type="text/html" href="/blogs/frankb/archive/2009/11/25/takeaways-from-pdc09.aspx" /><id>/blogs/frankb/archive/2009/11/25/takeaways-from-pdc09.aspx</id><published>2009-11-25T19:35:32Z</published><updated>2009-11-25T19:35:32Z</updated><content type="html">&lt;p&gt;So after a few days back home I got some time to think about the takeaways from PDC09.&lt;/p&gt;  &lt;p&gt;As you might have heard by now Microsoft did not have a lot of mayor announcements this year. Nothing of the scale of Windows Azure they came up with last year. They did release Windows Identity Foundation (FKA Geneva) and there is this thing they called Dallas, which is like a marketplace for online data that can be accessed by REST like services. But for the most of it it was more of what was already publically known.&lt;/p&gt;  &lt;p&gt;So what did I take home from LA.&amp;#160; For once it is good to see that Microsoft does not radically change its strategy, but instead continues on the road they set up in the last years. Further on there seems to be an increased focus on making things simpler and really make software development more productive. Visual Studio 2010 has some great new debugging features like intellitrace and the new features for debugging concurrent applications and the new features in the entity framework (Lazy loading / POCO support) will make it possible to get real productivity gains in in real life projects.&lt;/p&gt;  &lt;p&gt;So PDC09 was not as spectacular as we all might have hoped, but it certainly was worth my while.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.infosupport.com/aggbug.aspx?PostID=41146" width="1" height="1"&gt;</content><author><name>frankb</name><uri>http://blogs.infosupport.com/members/frankb/default.aspx</uri></author></entry><entry><title>Getting ready for PDC09</title><link rel="alternate" type="text/html" href="/blogs/frankb/archive/2009/11/16/getting-ready-for-pdc09.aspx" /><id>/blogs/frankb/archive/2009/11/16/getting-ready-for-pdc09.aspx</id><published>2009-11-16T14:01:00Z</published><updated>2009-11-16T14:01:00Z</updated><content type="html">&lt;p&gt;Last Saturday &lt;a href="http://blogs.infosupport.com/blogs/marcelv/" title="Marcel"&gt;Marcel&lt;/a&gt;, &lt;a href="http://blogs.infosupport.com/blogs/raimondb/" title="Raimond"&gt;Raimond&lt;/a&gt; and I arrived in LA to attend the Microsoft Professional Developers Conference 2009. We used Sunday to do some sightseeing and get used to the LA time zone. While I am not fully adapted yet I now have some time to kill before breakfast.&lt;/p&gt;
&lt;p&gt;Even though the schedule contains a lot of session I am really looking forward to (most time slots have more than one interesting title so I will have to choose), most of it does not seem to be all that world shocking. Most sessions look like logical steps in the direction that was set out the last few years. But we might as well get totally flabbergasted by tomorrows key-note, we will just wait and see.&lt;/p&gt;
&lt;p&gt;Today there are some pre-conference workshops, of which I plan to attend a Workshop called &amp;quot;Patterns of Parallel Programming&amp;quot;. I will keep you posted of anything worth your and my time.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.infosupport.com/aggbug.aspx?PostID=37335" width="1" height="1"&gt;</content><author><name>frankb</name><uri>http://blogs.infosupport.com/members/frankb/default.aspx</uri></author></entry><entry><title>New coordination datastructures in .Net 4.0 Beta 2</title><link rel="alternate" type="text/html" href="/blogs/frankb/archive/2009/11/12/new-coordination-datastructures-in-net-4-0-beta-2.aspx" /><id>/blogs/frankb/archive/2009/11/12/new-coordination-datastructures-in-net-4-0-beta-2.aspx</id><published>2009-11-12T08:39:00Z</published><updated>2009-11-12T08:39:00Z</updated><content type="html">&lt;p&gt;To be honest a have not spent too much time looking at .Net 4.0 beta 2 yet. But I do keep up with the &lt;a href="http://blogs.msdn.com/pfxteam/default.aspx" title="blogs"&gt;blogs&lt;/a&gt; concerning Parallel Extensions. Josh Philps has just &lt;a href="http://blogs.msdn.com/pfxteam/archive/2009/11/06/9918363.aspx" title="Posted"&gt;posted&lt;/a&gt; an entry about the changes that were made in the Coordination Data Structures (CDS) in Beta 2. The CDS are the types that have been added to the framework that will help writing concurrent applications without having to do (to much of) your own synchronization. I used two of the CDS classes (ConcurrentDictionary(Tkey, TVAlue) and Lazy&amp;lt;T&amp;gt;) in the CacheDictionary that I wrote about in &lt;a href="http://blogs.infosupport.com/blogs/frankb/archive/2008/12/31/Implementing-a-Thread-Safe-cache-using-the-Parallel-Extensions.aspx" title="this"&gt;this&lt;/a&gt; article.&lt;/p&gt;
&lt;div style="line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;max-height:350px;font-size:8pt;overflow:auto;cursor:text;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   1:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; CacheDictionary&amp;lt;TKey, TValue&amp;gt;   &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   2:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; TValue : &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#008000;"&gt;// needs to be a ref type due to current limitation of lazyInit&amp;lt;&amp;gt;   &lt;/span&gt;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   3:&lt;/span&gt; {   &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   4:&lt;/span&gt;     ConcurrentDictionary&amp;lt;TKey, LazyInit&amp;lt;TValue&amp;gt;&amp;gt; _cacheItemDictionary = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ConcurrentDictionary&amp;lt;TKey, LazyInit&amp;lt;TValue&amp;gt;&amp;gt;();   &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   5:&lt;/span&gt;   &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   6:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; TValue Fetch(TKey key, Func&amp;lt;TValue&amp;gt; producer)   &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   7:&lt;/span&gt;     {   &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   8:&lt;/span&gt;         LazyInit&amp;lt;TValue&amp;gt; cacheItem;   &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   9:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (!_cacheItemDictionary.TryGetValue(key, &lt;span style="color:#0000ff;"&gt;out&lt;/span&gt; cacheItem))   &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  10:&lt;/span&gt;         {   &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  11:&lt;/span&gt;             cacheItem = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; LazyInit&amp;lt;TValue&amp;gt;(() =&amp;gt; producer(), LazyInitMode.EnsureSingleExecution);   &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  12:&lt;/span&gt;   &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  13:&lt;/span&gt;             &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (!_cacheItemDictionary.TryAdd(key, cacheItem))   &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  14:&lt;/span&gt;             {   &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  15:&lt;/span&gt;                 &lt;span style="color:#008000;"&gt;// while we never remove items, if TryAdd fails it should be present   &lt;/span&gt;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  16:&lt;/span&gt;                 cacheItem = _cacheItemDictionary[key];   &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  17:&lt;/span&gt;             }   &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  18:&lt;/span&gt;         }   &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  19:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; cacheItem.Value;   &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  20:&lt;/span&gt;     }   &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  21:&lt;/span&gt; }  &lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In this cache I used ConcurrentDictionary&amp;lt;TKey, TValue&amp;gt; to store the cache items. To check if an item is present or should be created, I used TryAdd(key, cacheItem). This will return false if the item is allready present in the cache, in that case I assume the item can be retrieved from the dictionary with the indexer[]. This will work because the cache I implemented (unfortunately) does not yet support removals. If the cache would support any kind of mechanism to remove or invalidate items in the cache, the fetch method would have to handle the case of an item beeing removed between the TryAdd() and the retrieval. &lt;/p&gt;
&lt;p&gt;The problem here is that up till beta 2 there was no good way to do the Add and &amp;quot;Get if allready present&amp;quot; in a single atomic operation. To work around this I would have to either use my own lock (that would remove all benefits of using a CuncurrentDictionary in the first place) or create a loop that keeps on calling TryAdd() and TryGetValue() until one of them succeeds.&lt;/p&gt;
&lt;p&gt;I submitted this &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/37bbc361-6851-43db-9e90-80cc7e6ac15f" title="feedback"&gt;feedback&lt;/a&gt; to the parallel extensions forum with the suggestion to add an operation TryGetOrAddValue() that does these two operations in a single atomic method. I am pretty happy (and a bit proud :-) to see that this suggestion has found its way into the framework. The actual operation is now called GetorAdd(), (without the Try) which makes sense because either the Get or the Add will allways succeed. The PFX team really does seem to listen to custmer feedback!&lt;/p&gt;
&lt;div&gt;As a bonus they actually added an overload for GetOrAdd which accepts a delegate that is executed only if the item was not yet in the dictionary. I have not yet looked into the details, but it looks like this does exactly what my CacheDictionary.Fetch operation did! This means that (unless I add support for cache expiration) my CacheDictionary will no longer be needed at all, as all its functionality has now been incorporated into the Framework! Now how Cool is that&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.infosupport.com/aggbug.aspx?PostID=36691" width="1" height="1"&gt;</content><author><name>frankb</name><uri>http://blogs.infosupport.com/members/frankb/default.aspx</uri></author><category term=".Net" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/.Net/default.aspx" /><category term="C#" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/C_2300_/default.aspx" /><category term="parallel programming" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/parallel+programming/default.aspx" /><category term="PFX" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/PFX/default.aspx" /><category term="Cache" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Cache/default.aspx" /><category term="Thread Safe'" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Thread+Safe_2700_/default.aspx" /><category term=".Net 4.0" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/.Net+4.0/default.aspx" /></entry><entry><title>Visualizing Linq operators: The Info Support Linq Posters</title><link rel="alternate" type="text/html" href="/blogs/frankb/archive/2009/03/17/Visualizing-Linq-operators_3A00_-The-Info-Support-Linq-Posters.aspx" /><id>/blogs/frankb/archive/2009/03/17/Visualizing-Linq-operators_3A00_-The-Info-Support-Linq-Posters.aspx</id><published>2009-03-17T07:25:00Z</published><updated>2009-03-17T07:25:00Z</updated><content type="html">&lt;p&gt;Over the last few months I have been working on a little project to make Linq &amp;lsquo;come to life&amp;rsquo; in two full size posters that developers can hang on the wall of their offices. The Linq posters demonstrates the usage of all Linq operators using visual sequences of female and male figures. Since this week the final result is ready and can be ordered in Print / Downloaded from our website &lt;a href="http://www.infosupport.com/linq" title="http://www.infosupport.com/linq"&gt;&lt;font color="#669966"&gt;http://www.infosupport.com/linq&lt;/font&gt;&lt;/a&gt; free of charge! &lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="http://blogs.infosupport.com/photos/frankb/images/15326/original.aspx"&gt;&lt;img alt="poster_staand" border="0" height="285" src="http://blogs.infosupport.com/photos/frankb/images/15326/original.aspx" style="display:inline;border-width:0px;" title="poster_staand" width="202" /&gt;&lt;/a&gt;&amp;nbsp;&lt;a href="http://blogs.infosupport.com/photos/frankb/images/15327/original.aspx"&gt;&lt;img alt="poster_liggend" border="0" height="285" src="http://blogs.infosupport.com/photos/frankb/images/15327/original.aspx" style="display:inline;border-width:0px;" title="poster_liggend" width="401" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;So why create a Linq poster?&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Since even before C# 3.0 got out, a lot of articles and blog-posts have been written describing Linq. Most of these start with explaining the new C# language features: like extension Methods and Lambda expression and then explain how Linq is built on top of this, usually followed by the basics of comprehension syntax and the deferred execution model. What most of these articles don&amp;rsquo;t do, is give you a good overview of what is in the box, by that I mean the complete set of &lt;u&gt;50&lt;/u&gt;! operators that make up the Linq vocabulary. To really get all out of Linq that is in it I believe you should have a good sense of the available operators, because the real power of Linq is in it&amp;rsquo;s complete set of operators.&lt;/p&gt;&lt;p&gt;That is why we decided to create a set of two poster to visualize all available Linq operators (we could not fit all operators on a single poster at a reasonable scale). Each operator is demonstrated using a little sequence of colored female and male figures like the following example.&lt;/p&gt;&lt;p&gt;&lt;a href="http://blogs.infosupport.com/photos/frankb/images/15329/original.aspx"&gt;&lt;img alt="LinqPosterCrop" border="0" height="302" src="http://blogs.infosupport.com/photos/frankb/images/15329/original.aspx" style="display:inline;margin:0px 15px 0px 0px;border-width:0px;" title="LinqPosterCrop" width="516" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;If you would like to decorate your office (or bedroom) in Linq style, visit our website &lt;a href="http://www.infosupport.com/linq" title="http://www.infosupport.com/linq"&gt;&lt;font color="#669966"&gt;http://www.infosupport.com/linq&lt;/font&gt;&lt;/a&gt; to get your copies. &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.infosupport.com/aggbug.aspx?PostID=15325" width="1" height="1"&gt;</content><author><name>frankb</name><uri>http://blogs.infosupport.com/members/frankb/default.aspx</uri></author><category term=".Net" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/.Net/default.aspx" /><category term="C#" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/C_2300_/default.aspx" /><category term="Linq" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Linq/default.aspx" /><category term="Linq Poster" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Linq+Poster/default.aspx" /></entry><entry><title>CacheDictionary for .Net 3.5, using ReaderWriterLockSlim ?</title><link rel="alternate" type="text/html" href="/blogs/frankb/archive/2009/03/15/CacheDictionary-for-.Net-3.5_2C00_-using-ReaderWriterLockSlim-_3F00_.aspx" /><id>/blogs/frankb/archive/2009/03/15/CacheDictionary-for-.Net-3.5_2C00_-using-ReaderWriterLockSlim-_3F00_.aspx</id><published>2009-03-15T13:23:00Z</published><updated>2009-03-15T13:23:00Z</updated><content type="html">&lt;p&gt;In my previous &lt;a href="http://blogs.infosupport.com/blogs/frankb/archive/2008/12/31/Implementing-a-Thread-Safe-cache-using-the-Parallel-Extensions.aspx" target="_blank"&gt;post&lt;/a&gt; I described how to create a thread safe data cache using PFX. PFX however is scheduled to be &lt;a href="http://blogs.infosupport.com/blogs/frankb/archive/2008/10/16/Parallel-Extensions-for-.Net-Framework-will-be-included-in-.Net-4.0.aspx" target="_blank"&gt;released as a part of the .Net framework 4.0&lt;/a&gt; which means we will have to wait a while before we can use this in real world applications. That&amp;rsquo;s why I created an implementation using the generic Dictionary combined with a ReaderWriterLockSlim which are both available in .Net 3.5. today.&lt;/p&gt;&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;height:344px;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   1:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; CacheDictionary&amp;lt;TKey, TValue&amp;gt;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   3:&lt;/span&gt;     ReaderWriterLockSlim _cacheLock = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ReaderWriterLockSlim();&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   4:&lt;/span&gt;     Dictionary&amp;lt;TKey, LazyInit&amp;lt;TValue&amp;gt;&amp;gt; _cacheItemDictionary = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Dictionary&amp;lt;TKey,LazyInit&amp;lt;TValue&amp;gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   5:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   6:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; TValue Fetch(TKey key, Func&amp;lt;TValue&amp;gt; producer)&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   7:&lt;/span&gt;     {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   8:&lt;/span&gt;         LazyInit&amp;lt;TValue&amp;gt; cacheItem;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   9:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; found;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  10:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  11:&lt;/span&gt;         _cacheLock.EnterReadLock();&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  12:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;try&lt;/span&gt;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  13:&lt;/span&gt;         {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  14:&lt;/span&gt;             found = _cacheItemDictionary.TryGetValue(key, &lt;span style="color:#0000ff;"&gt;out&lt;/span&gt; cacheItem);&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  15:&lt;/span&gt;         }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  16:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;finally&lt;/span&gt;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  17:&lt;/span&gt;         {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  18:&lt;/span&gt;             _cacheLock.ExitReadLock();&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  19:&lt;/span&gt;         }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  20:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  21:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (!found)&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  22:&lt;/span&gt;         {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  23:&lt;/span&gt;             _cacheLock.EnterWriteLock();&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  24:&lt;/span&gt;             &lt;span style="color:#0000ff;"&gt;try&lt;/span&gt;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  25:&lt;/span&gt;             {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  26:&lt;/span&gt;                 &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (!_cacheItemDictionary.TryGetValue(key, &lt;span style="color:#0000ff;"&gt;out&lt;/span&gt; cacheItem))&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  27:&lt;/span&gt;                 {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  28:&lt;/span&gt;                     cacheItem = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; LazyInit&amp;lt;TValue&amp;gt;(producer);&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  29:&lt;/span&gt;                     _cacheItemDictionary.Add(key, cacheItem);&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  30:&lt;/span&gt;                 }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  31:&lt;/span&gt;             }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  32:&lt;/span&gt;             &lt;span style="color:#0000ff;"&gt;finally&lt;/span&gt;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  33:&lt;/span&gt;             {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  34:&lt;/span&gt;                 _cacheLock.ExitWriteLock();&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  35:&lt;/span&gt;             }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  36:&lt;/span&gt;         }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  37:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  38:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; cacheItem.Value;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  39:&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  40:&lt;/span&gt; }&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;This Implementation uses the (thread safe version of) the double-check locking pattern. This code first checks if the item exists using only a read lock, and only if the item is not found it acquire a write lock. To be sure the item was not added in between it checks again and adds the new item if it is still not found. Assuming there will be a fairly large amount of cache hits compared to cache misses, this is (in theory) more efficient than exclusively locking the dictionary right away, because multiple readers can get items from the cache concurrently and we only block other threads if we need to add a new item.&lt;/p&gt;&lt;p&gt;Just like in my previous implementation, I did not want to keeping the cache locked while actually retrieving or creating the item that needs to be cached. In the PFX version I did this by using LazyInit&amp;lt;T&amp;gt;, which will also be part of .Net 4.0 (They actually &lt;a href="http://blogs.msdn.com/pfxteam/archive/2009/03/04/9457880.aspx" target="_blank"&gt;just&lt;/a&gt; renamed it to Lazy&amp;lt;T&amp;gt;). Because I wanted the cache to work on .Net 3.5, I created my own implementation of LazyInit&amp;lt;T&amp;gt; which is not as versatile and highly optimized as the .Net 4.0 version, but does the job and is actually quite simple (see code below). While the cache-wide write lock is held, all I do is create an instance of LazyInit&amp;lt;T&amp;gt; and store that instance in the dictionary. After the cache-wide lock is released I call LazyInit.Value which will actually call the delegate to create the item as needed. LazyInit&amp;lt;T&amp;gt; uses a lock as well, but each instance has it&amp;rsquo;s own lock object, this way each instance will be initialized exactly once, but different items can be initialized concurrently.&lt;/p&gt;&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   1:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; LazyInit&amp;lt;T&amp;gt;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   3:&lt;/span&gt;     Func&amp;lt;T&amp;gt; _producer;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   4:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; _lock = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;object&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   5:&lt;/span&gt;     T _data;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   6:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;volatile&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; _created;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   7:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   8:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; LazyInit(Func&amp;lt;T&amp;gt; producer)&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   9:&lt;/span&gt;     {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  10:&lt;/span&gt;         _producer = producer;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  11:&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  12:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  13:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; T Value&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  14:&lt;/span&gt;     {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  15:&lt;/span&gt;         get&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  16:&lt;/span&gt;         {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  17:&lt;/span&gt;             &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (!_created)&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  18:&lt;/span&gt;             {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  19:&lt;/span&gt;                 &lt;span style="color:#0000ff;"&gt;lock&lt;/span&gt; (_lock)&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  20:&lt;/span&gt;                 {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  21:&lt;/span&gt;                     &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (!_created)&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  22:&lt;/span&gt;                     {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  23:&lt;/span&gt;                         _data = _producer.Invoke();&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  24:&lt;/span&gt;                         _created = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  25:&lt;/span&gt;                         _producer = &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  26:&lt;/span&gt;                     }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  27:&lt;/span&gt;                 }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  28:&lt;/span&gt;             }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  29:&lt;/span&gt;             &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _data;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  30:&lt;/span&gt;         }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  31:&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  32:&lt;/span&gt; }&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Actually being quite happy with this implementation which does a minimum of locking while still being thread safe I sat back and relaxed.&lt;/p&gt;&lt;p&gt;Only A short time after I created this implementation, a blog post by &lt;a href="http://www.bluebytesoftware.com/blog/default.aspx" target="_blank"&gt;Joe Duffy&lt;/a&gt;, the Lead developer / Architect behind PFX, showed up in my feed reader:&lt;/p&gt;&lt;h4&gt;&lt;a href="http://www.bluebytesoftware.com/blog/PermaLink,guid,0d4444a7-a678-447d-969e-61ca4ae3d34e.aspx"&gt;Reader/writer locks and their (lack of) applicability to fine-grained synchronization &lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/a&gt;&lt;/h4&gt;&lt;p&gt;In this post Joe compares the ReaderWriterLockSlim to (among others) a normal Mutex better known as the C# lock() statement. On a 4 core machine, when there are mostly readers and only a few writers, the reader/writer lock should theoretically be about 4 times as fast as the mutex. The reader / writer lock will allow multiple reader to execute concurrent on all four cores and the mutex only allows one thread to execute at a time leaving 3 cores idle while the other is holding the lock.&lt;/p&gt;&lt;p&gt;As it turns out, the mutex outperforms the ReaderWriterLockSlim in many scenario&amp;rsquo;s, especially in those cases where the locks are held for a relatively short time (like in a single peek in a dictionary). In stead of being 4 times faster the ReaderWriterLockSlim is actually about half as fast as the mutex! It seems that the internal book-keeping that needs to be done by the ReaderWriterLockSlim is way more expensive than the penalty of 3 cores being idle for a short time while 1 thread is holding the lock. Even Joe&amp;rsquo;s experimental highly optimized spinning ReaderWriterLock does not do a much better job in these scenario&amp;rsquo;s&lt;/p&gt;&lt;p&gt;ReaderWriterLockSlim start to outperform the mutex when the lock needs to be kept for a longer time (which you should generally avoid anyway). The number of cores will probably influence the results as well, I think running the same test on a 256 core box will increase the performance of the reader writer lock compared to the mutex, but unfortunately I have not been able to test this.&lt;/p&gt;&lt;p&gt;To test how this influenced my CacheDictionary, I created yet another implementation using a mutex in stead of the reader writer lock. Compared to the ReaderWriterLockSlim version this code is actually quite simple because it does not need to do the double check locking and the lock() statement does not require an explicit finally block. I did stick to the LazyInit aproach the avoid locking the whole cache while creating the items to cache.&lt;/p&gt;&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   1:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; CacheDictionary&amp;lt;TKey, TValue&amp;gt;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   3:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; _cacheLock = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;object&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   4:&lt;/span&gt;     Dictionary&amp;lt;TKey, LazyInit&amp;lt;TValue&amp;gt;&amp;gt; _cacheItemDictionary = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Dictionary&amp;lt;TKey,LazyInit&amp;lt;TValue&amp;gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   5:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   6:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; TValue Fetch(TKey key, Func&amp;lt;TValue&amp;gt; producer)&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   7:&lt;/span&gt;     {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   8:&lt;/span&gt;         LazyInit&amp;lt;TValue&amp;gt; cacheItem;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   9:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  10:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;lock&lt;/span&gt; (_cacheLock)&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  11:&lt;/span&gt;         {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  12:&lt;/span&gt;             &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (!_cacheItemDictionary.TryGetValue(key, &lt;span style="color:#0000ff;"&gt;out&lt;/span&gt; cacheItem))&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  13:&lt;/span&gt;             {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  14:&lt;/span&gt;                 cacheItem = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; LazyInit&amp;lt;TValue&amp;gt;(producer);&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  15:&lt;/span&gt;                 _cacheItemDictionary.Add(key, cacheItem);&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  16:&lt;/span&gt;             }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  17:&lt;/span&gt;         }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  18:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; cacheItem.Value;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  19:&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  20:&lt;/span&gt; }&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;I ran some simple performance test on my dual core machine to compare this code to the ReaderWriterLockSlim version. My results were similar to Joe&amp;rsquo;s, the version with the Mutex&amp;nbsp; is way faster then ReaderWriterLockSlim, even when there are only readers and not a single writer! The actual results depend on a lit of factors, like the number of items in the dictionary and of course the amount of work that is done in the delegate that produces the cache item, but in general the reader/writer version took about 1.5 times as long on the same scenario as the Mutex version. As in many cases: Simplicity wins!&lt;/p&gt;&lt;p&gt;For the near future, I will stay away from reader writer locks unless it is absolutely necessary to hold a lock for a fair amount of time. Maybe I will revisit this statement when commodity hardware reaches 32 or 64 cores.&amp;nbsp; &lt;/p&gt;&lt;p&gt;Joe Duffy concludes his post on this topic with: &amp;ldquo;&lt;em&gt;Sharing is &lt;strong&gt;evil&lt;/strong&gt;, fundamentally limits scalability, and is best avoided.&amp;rdquo; &lt;/em&gt;While in general I think he is right, the whole point of a cache is to share the result of a previous request with future requests in order to improve performance. Since caching implies sharing you should always be aware of the concurrency issues involved, that is why I tried to handle most of these issues in this generic utility.&lt;/p&gt;&lt;img src="http://blogs.infosupport.com/aggbug.aspx?PostID=15295" width="1" height="1"&gt;</content><author><name>frankb</name><uri>http://blogs.infosupport.com/members/frankb/default.aspx</uri></author><category term=".Net" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/.Net/default.aspx" /><category term="C#" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/C_2300_/default.aspx" /><category term="multi core" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/multi+core/default.aspx" /><category term="parallel programming" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/parallel+programming/default.aspx" /><category term="Algorithms" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Algorithms/default.aspx" /><category term="Cache" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Cache/default.aspx" /><category term="ReaderWriterLockSlim" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/ReaderWriterLockSlim/default.aspx" /></entry><entry><title>Implementing a Thread Safe cache using the Parallel Extensions</title><link rel="alternate" type="text/html" href="/blogs/frankb/archive/2008/12/31/Implementing-a-Thread-Safe-cache-using-the-Parallel-Extensions.aspx" /><id>/blogs/frankb/archive/2008/12/31/Implementing-a-Thread-Safe-cache-using-the-Parallel-Extensions.aspx</id><published>2008-12-31T14:19:00Z</published><updated>2008-12-31T14:19:00Z</updated><content type="html">&lt;p&gt;Caching objects based on a key is a very common task in software development, making it both thread safe and scalable however is not quite trivial. That&amp;#39;s why I implemented a generic implementation of such a cache, to handle the concurrency issues I used the Parallel Extensions to the .NET framework (PFX).&lt;/p&gt;&lt;p&gt;[Update: See &lt;a href="http://blogs.infosupport.com/blogs/frankb/archive/2009/03/15/CacheDictionary-for-.Net-3.5_2C00_-using-ReaderWriterLockSlim-_3F00_.aspx"&gt;this post&lt;/a&gt;&amp;nbsp;for a cache that works with .Net 3.5 without PFX]&lt;/p&gt;&lt;p&gt;A implementation pattern I have seen quite often, uses a dictionary to store cached items based on a cache key and looks like the following:&lt;/p&gt;&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:300px;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   1:&lt;/span&gt; Dictionary&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;, Item&amp;gt; _cachedItems = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;, Item&amp;gt;();&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   2:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; _lock = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;object&lt;/span&gt;(); &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   3:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   4:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Item GetItem(&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; id)&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   5:&lt;/span&gt; {    &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   6:&lt;/span&gt;     Item result;    &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   7:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(!_cacheItems.TryGetValue(id, &lt;span style="color:#0000ff;"&gt;out&lt;/span&gt; result))    &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   8:&lt;/span&gt;     {        &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   9:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;lock&lt;/span&gt;(_lock)        &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  10:&lt;/span&gt;         {            &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  11:&lt;/span&gt;             &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(!_cacheItems.TryGetValue(id, &lt;span style="color:#0000ff;"&gt;out&lt;/span&gt; result))&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  12:&lt;/span&gt;             {                &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  13:&lt;/span&gt;                 result = CreateItem(id); &lt;span style="color:#008000;"&gt;// does the actual expensive work of creating the item&lt;/span&gt;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  14:&lt;/span&gt;                 _cacheItems.Add(id, result);&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  15:&lt;/span&gt;             }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  16:&lt;/span&gt;         }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  17:&lt;/span&gt;     }    &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  18:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; result;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  19:&lt;/span&gt; }&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;This implementation uses a pattern known as &amp;#39;double check locking&amp;#39;, this allows for items already&amp;nbsp; in the cache to be retrieved without using a lock, only if the item is not found it acquires a lock, checks again for the item having been added by another thread between checking the first time and acquiring the lock, and then creates the item and stores it for future use.&lt;/p&gt;&lt;p&gt;There are some problems with this implementation. The first and most important is that the MSDN documentation guarantees the dictionary only to be thread safe for multiple concurrent readers, not for reads and a write at the same time. In this implementation the first TryGetValue is done outside the lock and could conflict with an Add from an other thread. To get this right you will need to use more sophisticated locking using for instance a ReaderWriterLockSlim or always lock the entire cache even when only reading. &lt;/p&gt;&lt;p&gt;Another problem is that the CreateItem() Method, which does the actual expensive work to get the data we want to cache (e.g. call a Web Service), is done inside the lock that synchronizes access to the cache. This means that even if multiple different items are requested, only one item will be created at a time, this can be killing the performance you wanted to &lt;em&gt;improve&lt;/em&gt; by caching. Finally this implementation requires the same pattern to be (wrongly) implemented over and over again because the code to create an item usually differs for every scenario.&lt;/p&gt;&lt;p&gt;To solve these problems once and for all I created a generic CacheDictionary using PFX. This CacheDictionary does not allow for items to be directly added or retrieved, instead the Fetch() method takes the key of the requested item and a delegate that will create the item if needed. If the item is found in the cache it will be returned, if not the provided delegate is invoked to create the item and store it for later use. &lt;/p&gt;&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:350px;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   1:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; CacheDictionary&amp;lt;TKey, TValue&amp;gt;   &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   2:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; TValue : &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#008000;"&gt;// needs to be a ref type due to current limitation of lazyInit&amp;lt;&amp;gt;   &lt;/span&gt;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   3:&lt;/span&gt; {   &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   4:&lt;/span&gt;     ConcurrentDictionary&amp;lt;TKey, LazyInit&amp;lt;TValue&amp;gt;&amp;gt; _cacheItemDictionary = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ConcurrentDictionary&amp;lt;TKey, LazyInit&amp;lt;TValue&amp;gt;&amp;gt;();   &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   5:&lt;/span&gt;   &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   6:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; TValue Fetch(TKey key, Func&amp;lt;TValue&amp;gt; producer)   &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   7:&lt;/span&gt;     {   &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   8:&lt;/span&gt;         LazyInit&amp;lt;TValue&amp;gt; cacheItem;   &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   9:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (!_cacheItemDictionary.TryGetValue(key, &lt;span style="color:#0000ff;"&gt;out&lt;/span&gt; cacheItem))   &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  10:&lt;/span&gt;         {   &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  11:&lt;/span&gt;             cacheItem = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; LazyInit&amp;lt;TValue&amp;gt;(() =&amp;gt; producer(), LazyInitMode.EnsureSingleExecution);   &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  12:&lt;/span&gt;   &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  13:&lt;/span&gt;             &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (!_cacheItemDictionary.TryAdd(key, cacheItem))   &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  14:&lt;/span&gt;             {   &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  15:&lt;/span&gt;                 &lt;span style="color:#008000;"&gt;// while we never remove items, if TryAdd fails it should be present   &lt;/span&gt;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  16:&lt;/span&gt;                 cacheItem = _cacheItemDictionary[key];   &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  17:&lt;/span&gt;             }   &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  18:&lt;/span&gt;         }   &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  19:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; cacheItem.Value;   &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  20:&lt;/span&gt;     }   &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  21:&lt;/span&gt; }  &lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;To store the items I used the ConcurrentDictionary&amp;lt;TKey, TValue&amp;gt; provided with the September 2008 CTP of PFX. Internally the ConcurrentDictionary does all the required synchronization magic so I don&amp;#39;t need to write a single lock myself. The ConcurrentDictionary looks a bit like the normal Dictionary&amp;lt;TKey, TValue&amp;gt;, but the methods are a bit different. In does for instance not support Add() and Remove() methods, but only TryAdd() and TryRemove(). This is because with a ConcurrentDictionary you can never be sure of it&amp;#39;s state while other threads may be adding or removing items at any moment.&lt;/p&gt;&lt;p&gt;To use the ConcurrentDictionary as a store for the cache the first step is to &lt;strong&gt;Try &lt;/strong&gt;to Get the item with a TryGetValue(). If it is not found the new item is &amp;#39;created&amp;#39; and I &lt;strong&gt;Try &lt;/strong&gt;to Add it to the cache. This might fail if another thread beat me to it, in that case I can be sure the item exists (I don&amp;#39;t support removes yet) and just get it using the indexer.&lt;/p&gt;&lt;p&gt;Inside the ConcurrentDictionary I don&amp;#39;t store the cached items directly. Instead I wrap the actual items in another class introduced by the PFX, LazyInit&amp;lt;T&amp;gt;. LazyInit&amp;lt;T&amp;gt; allows a very simple implementation of lazy object initialization. When creating an instance of LaziInt&amp;lt;T&amp;gt; the constructor expects a delegate that will create the item as needed. In this case I use the delegate that was provided in the call to Fetch() (due to some issues in the current CTP it can&amp;#39;t be passed directly). When the LazyInit.Value property is called for the first time, the delegate is executed and the result is stored in this instance of LazyInit for any subsequent calls. Internally LazyInit has it&amp;#39;s own synchronization logic which makes sure each item is created only once, this synchronization is done per instance of the LazyInit class. By using LazyInit here I&amp;#39;m sure each cache item is created only once without holding a lock for the the whole cache while doing so. Furthermore it allows me to &amp;#39;try&amp;#39; to store the instance of the LazyInit object in the cache before actually creating the item itself. Only If the TryAdd() succeeds this instance of LazyInit is actualy used to create the item.&lt;/p&gt;&lt;p&gt;&lt;u&gt;Note:&lt;/u&gt;&lt;em&gt; &lt;/em&gt;In the current CTP of PFX, LazyInt&amp;lt;T&amp;gt; has some limitations, the most important one is that it will only allows T to be a reference type. That is why this CacheDictionary currently also requires TValue to be a reference type. It is expected that this limitation will be removed when PFX ships. &lt;/p&gt;&lt;p&gt;This CacheDictionary currently does not support expiration policies like for instance the ASP.Net web cache does. I found the ASP.Net cache however not to be very versatile while it only has one global cache store for an entire AppDomain and only allows string keys. Maybe pluggable expiration policies will be something for a next version of CacheDictionary, there are however a lot of scenario&amp;#39;s where expiration is not really an issue and it is of course always possible to flush the entire CacheDictionary by just creating a new instance :-). Any suggestions for improvement however are welcome.&lt;/p&gt;&lt;img src="http://blogs.infosupport.com/aggbug.aspx?PostID=14888" width="1" height="1"&gt;</content><author><name>frankb</name><uri>http://blogs.infosupport.com/members/frankb/default.aspx</uri></author><category term=".Net" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/.Net/default.aspx" /><category term="C#" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/C_2300_/default.aspx" /><category term="Generics" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Generics/default.aspx" /><category term="Lambda expressions" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Lambda+expressions/default.aspx" /><category term="PFX" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/PFX/default.aspx" /><category term="Thread Safe" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Thread+Safe/default.aspx" /><category term="Cache" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Cache/default.aspx" /></entry><entry><title>F# to ship as part of Visual Studio 2010</title><link rel="alternate" type="text/html" href="/blogs/frankb/archive/2008/12/11/F_2300_-to-ship-as-part-of-Visual-Studio-2010.aspx" /><id>/blogs/frankb/archive/2008/12/11/F_2300_-to-ship-as-part-of-Visual-Studio-2010.aspx</id><published>2008-12-11T07:46:29Z</published><updated>2008-12-11T07:46:29Z</updated><content type="html">&lt;p&gt;To be honest I don't really like to blog about stuff that is already 'out there' on the web. There are however some topics that I personally think are important and don't always get the attention they deserve. These topics mostly concentrate around parallel programming (see my blogroll in the sidebar). So for these topics I will make an exception.&lt;/p&gt;  &lt;p&gt;Today Don Syme announced that &lt;a href="http://blogs.msdn.com/dsyme/archive/2008/12/10/fsharp-to-ship-as-part-of-visual-studio-2010.aspx" target="_blank"&gt;F# will be released with Visual Studio 2010&lt;/a&gt;. I had expected this news to get out at the PDC last October, but for some reason we were kept in the dark a little longer. &lt;/p&gt;  &lt;p&gt;I think this is a mayor step in bringing functional programming to mainstream software development. &lt;/p&gt;&lt;img src="http://blogs.infosupport.com/aggbug.aspx?PostID=14855" width="1" height="1"&gt;</content><author><name>frankb</name><uri>http://blogs.infosupport.com/members/frankb/default.aspx</uri></author><category term=".Net" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/.Net/default.aspx" /><category term="F#" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/F_2300_/default.aspx" /></entry><entry><title>More context information in Linq queries</title><link rel="alternate" type="text/html" href="/blogs/frankb/archive/2008/12/02/More-context-information-in-Linq-queries.aspx" /><id>/blogs/frankb/archive/2008/12/02/More-context-information-in-Linq-queries.aspx</id><published>2008-12-02T10:28:00Z</published><updated>2008-12-02T10:28:00Z</updated><content type="html">&lt;p&gt;Last week I &lt;a href="http://blogs.infosupport.com/blogs/frankb/archive/2008/11/23/Selecting-or-filtering-on-the-sequence-number-in-Linq.aspx" target="_blank"&gt;posted&lt;/a&gt; on how to use an elements sequence number in Linq queries. In the project I just finished working on, we wrote a lot of Linq queries where we needed even more information related to an elements position in the sequence, like comparing it to the the element that came directly before or directly after it. &lt;/p&gt;&lt;p&gt;To be concrete: the input sequence contains all (ordered) historical versions of the same insurance policy object. From this sequence we need to filter all items for which a specific property has changed since the previous version. This could be implemented like:&lt;/p&gt;&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.64%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;height:177px;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   1:&lt;/span&gt; Version previous = &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   2:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   3:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var version &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; Versions)&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   4:&lt;/span&gt; {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   5:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (previous != &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; previous.amount != version.amount)&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   6:&lt;/span&gt;     {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   7:&lt;/span&gt;         &lt;span style="color:#008000;"&gt;// do something with this item&lt;/span&gt;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   8:&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   9:&lt;/span&gt;     previous = version;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  10:&lt;/span&gt; }&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Once you have gotten addicted to the declarative style of Linq, writing code like this just doesn&amp;#39;t cut it anymore. I wanted to be able to use the filtered items as a sequence so it can be used in a Linq query.&lt;/p&gt;&lt;p&gt;Using the pattern I described last week this could be written like &lt;/p&gt;&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:98.59%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;height:49px;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   1:&lt;/span&gt; Versions.select((item, index) =&amp;gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; {item, index}).Skip(1)&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   2:&lt;/span&gt;         .&lt;span style="color:#0000ff;"&gt;where&lt;/span&gt;(v=&amp;gt;Versions.ElementAt(v.index - 1).amount != v.item.amount)&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;This query finds each items index number and uses it to find the preceding item using the ElementAt() method. This makes it possible to compare each item to it&amp;#39;s previous item in the where clause. The Skip(1) is needed because the first element obviously doesn&amp;#39;t have a previous item.&lt;/p&gt;&lt;p&gt;The ElementAt() method however is not very efficient, because in most cases to find a single item it iterates the sequence from the begin to the requested item. Finding each item&amp;#39;s preceding item by re-looping the sequence again can get you into serious performance issues when using larger sets.&lt;/p&gt;&lt;p&gt;Because I needed this kind of queries a lot in this project, I wanted to make then both easy to write and efficient in execution. So I wrote an extension method I called WithContext() that wraps each input item in a container object, along with some information about its context in the sequence. This allows for the code above to be written as:&lt;/p&gt;&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;height:36px;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   1:&lt;/span&gt; Versions.WithContext().Where(v=&amp;gt;v.Previous != &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; v.Previous.Amount != v.Current.Amount)&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;To select the duration of each version, based on its own StartDate and the StartDate of the next item, I can now write:&lt;/p&gt;&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   1:&lt;/span&gt; Versions.WithContext().Select(v.Next.StartDate - v.Current.StartDate)&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;WithContext() takes an IEnumerable&amp;lt;T&amp;gt; and returns an IEnumerable&amp;lt;ElementWithContext&amp;lt;T&amp;gt;&amp;gt;. ElementWithContext is a simple class that provides properties to retrieve the Current item as well as the Previous and the Next. While at it I added properties to get all Preceding and all Following items, as well as the sequence number.&lt;/p&gt;&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;height:250px;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   1:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ElementWithContext&amp;lt;T&amp;gt;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   3:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; T Previous { get; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; set; }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   4:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; T Current { get; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; set; }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   5:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; T Next { get; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; set; }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   6:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; Index { get; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; set; }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   7:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   8:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; Preceding { get; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; set; }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   9:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; Following { get; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; set; }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  10:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  11:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;internal&lt;/span&gt; ElementWithContext(T previous, T current, T next,&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  12:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; index, IEnumerable&amp;lt;T&amp;gt; preceding, IEnumerable&amp;lt;T&amp;gt; following)&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  13:&lt;/span&gt;     {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  14:&lt;/span&gt;         Current = current;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  15:&lt;/span&gt;         Previous = previous;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  16:&lt;/span&gt;         Next = next;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  17:&lt;/span&gt;         Index = index;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  18:&lt;/span&gt;         Preceding = preceding;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  19:&lt;/span&gt;         Following = following;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  20:&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  21:&lt;/span&gt; }&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;The implementation of WithContext() looks a bit like the code in the first sample. It loops the input sequence and for each element it yields a new ElementWithContext. To find the next element I did a kind of &amp;#39;look ahead&amp;#39; in the for loop. To do this I tweaked the input sequence so that it represents all &amp;#39;Next&amp;#39; items&amp;#39; . I did this by first adding an empty element at the end (the last item does not have a Next item) and then skip the first item (the first &amp;#39;next&amp;#39; item is the second in the sequence). This way WithContext() conforms to Linq&amp;#39;s &amp;#39;deferred execution&amp;#39; model by not taking more items from the input then necessary.&lt;/p&gt;&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:98.61%;cursor:text;max-height:300px;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;height:310px;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   1:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; IEnumerable&amp;lt;ElementWithContext&amp;lt;T&amp;gt;&amp;gt; WithContext&amp;lt;T&amp;gt;(&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; source)&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   3:&lt;/span&gt;     &lt;span style="color:#008000;"&gt;// initialize the previous and current item for the first source element&lt;/span&gt;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   4:&lt;/span&gt;     T previous = &lt;span style="color:#0000ff;"&gt;default&lt;/span&gt;(T);&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   5:&lt;/span&gt;     T current = source.FirstOrDefault();&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   6:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; index = 0;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   7:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   8:&lt;/span&gt;     &lt;span style="color:#008000;"&gt;// Loop all &amp;#39;Next&amp;#39; items&lt;/span&gt;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;   9:&lt;/span&gt;     &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (T next &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; source.Union(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;[] { &lt;span style="color:#0000ff;"&gt;default&lt;/span&gt;(T) }).Skip(1))&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  10:&lt;/span&gt;     {&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  11:&lt;/span&gt;         &lt;span style="color:#0000ff;"&gt;yield&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ElementWithContext&amp;lt;T&amp;gt;(previous, current, next,&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  12:&lt;/span&gt;                 index, source.Take(index), source.Skip(index + 1));&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  13:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  14:&lt;/span&gt;         previous = current;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  15:&lt;/span&gt;         current = next;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  16:&lt;/span&gt;         index++;&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  17:&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#606060;"&gt;  18:&lt;/span&gt; }&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;One more item that I&amp;#39;ll keep handy on my personal utility belt :-) &lt;/p&gt;&lt;p&gt;Oh, and thanks to &lt;a href="http://blogs.infosupport.com/ernow/default.aspx" target="_blank"&gt;Erno&lt;/a&gt; for pointing me to this Live Writer &lt;a href="http://gallery.live.com/liveItemDetail.aspx?li=d4409446-af7f-42ec-aa20-78aa5bac4748&amp;amp;l=8" target="_blank"&gt;Add-In&lt;/a&gt; for the code snippets. I hope this helps reading these way to long code samples (sorry for that).&lt;/p&gt;&lt;img src="http://blogs.infosupport.com/aggbug.aspx?PostID=14804" width="1" height="1"&gt;</content><author><name>frankb</name><uri>http://blogs.infosupport.com/members/frankb/default.aspx</uri></author></entry><entry><title>Selecting or filtering on the sequence number in Linq</title><link rel="alternate" type="text/html" href="/blogs/frankb/archive/2008/11/23/Selecting-or-filtering-on-the-sequence-number-in-Linq.aspx" /><id>/blogs/frankb/archive/2008/11/23/Selecting-or-filtering-on-the-sequence-number-in-Linq.aspx</id><published>2008-11-23T16:34:00Z</published><updated>2008-11-23T16:34:00Z</updated><content type="html">&lt;p&gt;A few weeks ago I was writing a Linq query where I needed each items sequence number (or index) as a part of the filter expression in a Where operation. To simplify the actual problem let&amp;#39;s say I needed to filter out only the odd items. One (wrong) way to do this would be:&lt;/p&gt;&lt;p&gt;&lt;br /&gt;int i = 0;&lt;br /&gt;var filtered = input.Where(x =&amp;gt; i++ % 2 == 1);&lt;/p&gt;&lt;p&gt;This will appear to work at first but it only works correct the first time the filtered sequence is iterated. I will not get into this to deep here, but it has to do with the lambda updating the outer variable which is a bad idea.&lt;br /&gt;Fortunately there is a correct (and easier) way to do this. As it turns out the Where extension method has an extra overload that excepts a lambda function with two input parameters. When you use this overload the lambda receives an extra input parameter which indicates the sequence number of the current item. The correct code looks like this.&lt;/p&gt;&lt;p&gt;var filtered = input.Where((x, i) =&amp;gt; i % 2 == 1);&lt;/p&gt;&lt;p&gt;As I wanted to write the rest of the query in Linq comprehension syntax (yes it does have some advantageous sometimes), I wanted to write something like&lt;/p&gt;&lt;p&gt;var filtered = from x in input&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // other Linq stuff&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where ???? % 2 == 1&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select x;&lt;/p&gt;&lt;p&gt;Comprehension syntax however does not have a built in way to refer to the sequence number (as far as I could find out in 15 min with Google). Bummer :-(&lt;/p&gt;&lt;p&gt;I did find that the Select method has an overload just like the Where method, that accepts a lambda with a sequence number parameter. This allows the sequence number to be used in the query&amp;#39;s output.&lt;/p&gt;&lt;p&gt;var withSequenceNumber = input.select((item, index) =&amp;gt; new {item, index})&lt;/p&gt;&lt;p&gt;Just like in the where clause, it is not possible to use the sequence number in comprehension syntax&amp;#39;s select clause. It is however possible to mix normal C# syntax and comprehension syntax and make the sequence number available to all the comprehension syntax operators:&lt;/p&gt;&lt;p&gt;var filtered = from x in input.select((item, index) =&amp;gt; new {item, index})&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // other Linq stuff&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where x.index %2 == 1&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select x.item;&lt;/p&gt;&lt;p&gt;By first selecting each item along with its sequence number into an anonymous type, the rest of the query can refer to the sequence number as x.index and the item itself as x.item. As an extra bonus the original sequence numbers of the items before filtering are still available after filtering, so they could be used in the select clause to be presented in the ouput.&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.infosupport.com/aggbug.aspx?PostID=14767" width="1" height="1"&gt;</content><author><name>frankb</name><uri>http://blogs.infosupport.com/members/frankb/default.aspx</uri></author><category term=".Net" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/.Net/default.aspx" /><category term="C#" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/C_2300_/default.aspx" /><category term="Sequence number" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Sequence+number/default.aspx" /><category term="Linq" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Linq/default.aspx" /><category term="Index number" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Index+number/default.aspx" /></entry><entry><title>Parallel Extensions for .Net Framework will be included in .Net 4.0</title><link rel="alternate" type="text/html" href="/blogs/frankb/archive/2008/10/16/Parallel-Extensions-for-.Net-Framework-will-be-included-in-.Net-4.0.aspx" /><id>/blogs/frankb/archive/2008/10/16/Parallel-Extensions-for-.Net-Framework-will-be-included-in-.Net-4.0.aspx</id><published>2008-10-16T07:24:00Z</published><updated>2008-10-16T07:24:00Z</updated><content type="html">&lt;p&gt;I has been&amp;nbsp;a while since I wrote a &lt;a href="http://blogs.infosupport.com/blogs/frankb/archive/2007/07/29/Will-multi_2D00_core-processing-change-the-way-we-write-software_3F00_.aspx" target="_blank"&gt;post&lt;/a&gt;&amp;nbsp;on parallel programming. Since that time a lot has happend in field of parallel programming.&lt;/p&gt;&lt;p&gt;Microsoft has been working hard on implementing the Parallel Extensions for .Net Framewok (PFX) and has now anounced that it will be a core part of the next version of the .Net Framework (right into mscorlib.dll). See the post on the PFX blog &lt;a href="http://blogs.msdn.com/pfxteam/archive/2008/10/10/8994927.aspx" target="_blank"&gt;PFX blog &lt;/a&gt;. If you are interested in Parallel programming I strongly recomend reading up on the PFX blog. I also expect more details to be anounced at the upcoming PDC (to bad I won&amp;#39;t be there).&lt;/p&gt;&lt;p&gt;FPX includes a task scheduler that promisses to scale well beyond two of four CPU&amp;#39;s / cores and allowes LINQ to objects queries to be executed parallel (PLINQ). The later, and probably lesser known aditions include so called Coordination Data Structures (CDS) these include thread safe collection classes, like stacks an queues.&amp;nbsp;One of my personal favorites of the CDS is LazyInit&amp;lt;T&amp;gt;. LazyInit is a smart little helper class that makes it easy to implement Lazy Initialization of fields or local variables, of course this is done thread safe so that it can be used in parallel scenario&amp;#39;s.&lt;/p&gt;&lt;p&gt;Can&amp;#39;t wait to use these new goodies in real life projects!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.infosupport.com/aggbug.aspx?PostID=14553" width="1" height="1"&gt;</content><author><name>frankb</name><uri>http://blogs.infosupport.com/members/frankb/default.aspx</uri></author><category term=".Net" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/.Net/default.aspx" /><category term="C#" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/C_2300_/default.aspx" /><category term="multi core" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/multi+core/default.aspx" /><category term="parallel programming" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/parallel+programming/default.aspx" /><category term="PFX" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/PFX/default.aspx" /></entry><entry><title>More fun with Extension Methods and Lambda's: Creating a generic tree visitor</title><link rel="alternate" type="text/html" href="/blogs/frankb/archive/2008/06/06/More-fun-with-Extension-Methods-and-Lambda_2700_s_3A00_-creating-a-generic-tree-visitor.aspx" /><id>/blogs/frankb/archive/2008/06/06/More-fun-with-Extension-Methods-and-Lambda_2700_s_3A00_-creating-a-generic-tree-visitor.aspx</id><published>2008-06-06T06:10:00Z</published><updated>2008-06-06T06:10:00Z</updated><content type="html">&lt;p&gt;One of the most common data structures in Software Engineering is the tree. In the .Net framework for instance you can find them in the file system, the controls of a win-forms application, nodes in an XML document etc.&lt;/p&gt;&lt;p&gt;A common task with trees is to traverse all nodes to look for a specific node or perform an operation on each node. This is usually called visiting. Many tree like data structures unfortunately don&amp;#39;t have a built-in visiting mechanism, so we usually need to build this ourselves with something like a recursive method.&lt;/p&gt;&lt;p&gt;What&amp;nbsp;I wanted to do is to write an generic implementation to visit any recursive tree. While the basic algorithm to do this is not very complicated, there are two factors that make it difficult to make the implementation generic, the first is that the type of the nodes is different for each tree, the second is that the way to obtain the children of a node is different for each tree. C# 2.0 solved the first problem with generics, and now C# 3.0 has solved the second problem with Lambda expressions.&lt;/p&gt;&lt;p&gt;A generic tree visitor should allow me to iterate all nodes as a single Enumerable&amp;lt;T&amp;gt;, which of course has the advantage that it can be used it a foreach or a Linq query. So the code I Would like to write to visit a tree would be something like the following:&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;var&lt;/span&gt; node &lt;span style="color:blue;"&gt;in&lt;/span&gt; root.VisitDepthFirst(n =&amp;gt; n.GetChildNodes()))&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;{&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:green;"&gt;// ...&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;}&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 10pt;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 10pt;"&gt;Where VisitDepthFirst is a generic extension method that operates on the root object. The extension method receives a Lambda expression that is used on a node to retrieve all it&amp;#39;s children. For most trees this is a single property or method which return a collection of all child objects.&lt;/p&gt;&lt;p&gt;See the implementation of the depth first visitor below&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; VisitDepthFirst&amp;lt;T&amp;gt;(&lt;span style="color:blue;"&gt;this&lt;/span&gt; T root, &lt;span style="color:#2b91af;"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color:#2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; childSelector)&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;{&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;if&lt;/span&gt; (root == &lt;span style="color:blue;"&gt;null&lt;/span&gt;) &lt;span style="color:blue;"&gt;yield&lt;/span&gt; &lt;span style="color:blue;"&gt;break&lt;/span&gt;;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:green;"&gt;// we return the root&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;yield&lt;/span&gt; &lt;span style="color:blue;"&gt;return&lt;/span&gt; root;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:green;"&gt;// then we do a recursive Depth first search of all children and yield each item we find&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;var&lt;/span&gt; child &lt;span style="color:blue;"&gt;in&lt;/span&gt; childSelector(root).SelectMany(c =&amp;gt; c.VisitDepthFirst(childSelector)))&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;yield&lt;/span&gt; &lt;span style="color:blue;"&gt;return&lt;/span&gt; child;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;}&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;&lt;/font&gt;&lt;/p&gt;&lt;p&gt;When testing this in a Win-forms application I first tried the following&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;var&lt;/span&gt; control &lt;span style="color:blue;"&gt;in&lt;/span&gt; groupBox2.VisitDepthFirst(c =&amp;gt; c.Controls))&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;{&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;control.Text = &lt;span style="color:#a31515;"&gt;&amp;quot;Done&amp;quot;&lt;/span&gt;;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;}&lt;/span&gt;&lt;/p&gt;&lt;p&gt;This however leads to a a few compiler errors. The first is has to do with the fact that groupBox2 is of type &lt;span style="color:#2b91af;"&gt;&lt;font face="Courier New"&gt;GroupBox &lt;/font&gt;&lt;/span&gt;and not &lt;span style="color:#2b91af;"&gt;&lt;font face="Courier New"&gt;Control &lt;/font&gt;&lt;/span&gt;therefore the compiler expects the Lambda expression to return &lt;span style="color:#2b91af;"&gt;&lt;font face="Courier New"&gt;IEnumerable&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;&lt;font face="Courier New"&gt;GroupBox &lt;/font&gt;&lt;/span&gt;&amp;gt; instead of &lt;span style="color:#2b91af;"&gt;&lt;font face="Courier New"&gt;IEnumerable&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;&lt;font face="Courier New"&gt;Control&lt;/font&gt;&lt;/span&gt;&amp;gt;. The second problem is that the ControlsCollection does not implement a generic &lt;span style="color:#2b91af;"&gt;&lt;font face="Courier New"&gt;IEnumerable&amp;lt;&amp;gt; &lt;/font&gt;&lt;/span&gt;at all but in stead only the non-generic &lt;span style="color:#2b91af;"&gt;&lt;font face="Courier New"&gt;IEnumerable&lt;/font&gt;&lt;/span&gt;.&lt;/p&gt;&lt;p&gt;To fix this I had to be a bit more explicit about the type of the node and use Linq&amp;#39;s Cast&amp;lt;&amp;gt;() Extension method to convert the Controls collection to &lt;span style="color:#2b91af;"&gt;&lt;font face="Courier New"&gt;IEnumerable&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;&lt;font face="Courier New"&gt;Control&lt;/font&gt;&lt;/span&gt;&amp;gt;. The following is the correct way to do this.&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;var&lt;/span&gt; control &lt;span style="color:blue;"&gt;in&lt;/span&gt; groupBox2.VisitDepthFirst&amp;lt;&lt;span style="color:#2b91af;"&gt;Control&lt;/span&gt;&amp;gt;(c =&amp;gt; c.Controls.Cast&amp;lt;&lt;span style="color:#2b91af;"&gt;Control&lt;/span&gt;&amp;gt;()))&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;control.Text = &lt;span style="color:#a31515;"&gt;&amp;quot;Done&amp;quot;&lt;/span&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;}&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Likewise visiting all nodes in an xml document is done as follows&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;XmlNode&lt;/span&gt; childNode &lt;span style="color:blue;"&gt;in&lt;/span&gt; xmlDocument.VisitDepthFirst&amp;lt;&lt;span style="color:#2b91af;"&gt;XmlNode&lt;/span&gt;&amp;gt;(n =&amp;gt; n.ChildNodes.Cast&amp;lt;&lt;span style="color:#2b91af;"&gt;XmlNode&lt;/span&gt;&amp;gt;()))&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;{&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;}&lt;/span&gt;&lt;/p&gt;&lt;p&gt;To be complete I also created a breath-first search. I don&amp;#39;t know if I&amp;#39;ll ever use it, but why not.&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; VisitBreathFirst&amp;lt;T&amp;gt;(&lt;span style="color:blue;"&gt;this&lt;/span&gt; T root, &lt;span style="color:#2b91af;"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color:#2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; childSelector)&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;{&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span style="color:blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if&lt;/span&gt; (root == &lt;span style="color:blue;"&gt;null&lt;/span&gt;) &lt;span style="color:blue;"&gt;yield&lt;/span&gt; &lt;span style="color:blue;"&gt;break&lt;/span&gt;;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Queue&lt;/span&gt;&amp;lt;T&amp;gt; open = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Queue&lt;/span&gt;&amp;lt;T&amp;gt;(); &lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:green;"&gt;// the open queue contains the items that have already been visited, but who&amp;#39;s children have not&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;color:green;font-family:'Courier New';"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;yield&lt;/span&gt; &lt;span style="color:blue;"&gt;return&lt;/span&gt; root;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;open.Enqueue(root);&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;while&lt;/span&gt; (open.Count &amp;gt;0)&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;T item = open.Dequeue();&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (T child &lt;span style="color:blue;"&gt;in&lt;/span&gt; childSelector(item))&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;yield&lt;/span&gt; &lt;span style="color:blue;"&gt;return&lt;/span&gt; child;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;open.Enqueue(child);&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;line-height:normal;"&gt;&lt;span style="font-size:10pt;font-family:'Courier New';"&gt;}&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;&lt;/font&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;While I think&amp;nbsp;this is a nice example of generics and Lambda&amp;#39;s,&amp;nbsp;I have to be hones that I&amp;#39;m not quite sure if this should have been implemented as Extension Methods. The problem here is that these extension methods operate on any object without restriction, which makes them pop up in intellisense all over the place once you add the using directive.&amp;nbsp;The alternative ofcource is to make it a normal static method and loose the syntactic sugar of extension methods.&amp;nbsp;&lt;/font&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;Any comments on that one?&lt;/font&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.infosupport.com/aggbug.aspx?PostID=13952" width="1" height="1"&gt;</content><author><name>frankb</name><uri>http://blogs.infosupport.com/members/frankb/default.aspx</uri></author><category term="C#" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/C_2300_/default.aspx" /><category term="Generics" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Generics/default.aspx" /><category term="Lambda expressions" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Lambda+expressions/default.aspx" /><category term="C# 3.0" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/C_2300_+3.0/default.aspx" /><category term="Extension methods" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Extension+methods/default.aspx" /><category term="Algorithms" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Algorithms/default.aspx" /><category term="Visitor" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Visitor/default.aspx" /></entry><entry><title>Using C# 3.0 Extension methods and Lambda expressions to avoid nasty Null Checks</title><link rel="alternate" type="text/html" href="/blogs/frankb/archive/2008/02/02/Using-C_2300_-3.0-Extension-methods-and-Lambda-expressions-to-avoid-nasty-Null-Checks.aspx" /><id>/blogs/frankb/archive/2008/02/02/Using-C_2300_-3.0-Extension-methods-and-Lambda-expressions-to-avoid-nasty-Null-Checks.aspx</id><published>2008-02-02T19:14:00Z</published><updated>2008-02-02T19:14:00Z</updated><content type="html">&lt;p&gt;In the project I am currently working on we use a domain model with a quite deep nested structure of classes, code like the following is quite common.&lt;/p&gt;&lt;p&gt;// do something with&amp;nbsp;the Adress&lt;br /&gt;Console.Writeline(Contract.Parties.Client.Adress.Street);&lt;/p&gt;&lt;p&gt;The problem with this is that not all Contacts have a related Parties object and if we do have a Parties object sometimes the Client object is Null. This lead to writing a lot of code code like the following:&lt;/p&gt;&lt;p&gt;if (Conract != Null &amp;amp;&amp;amp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Contract.Parties != null &amp;amp;&amp;amp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Contract.Parties.Client != null &amp;amp;&amp;amp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Contract.Parties.Client.Adress != null)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // do something with&amp;nbsp;the Adress&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.Writeline(Contract.Parties.Client.Adress.Street);&lt;br /&gt;}&lt;/p&gt;&lt;p&gt;There are a few problems with this code, the first of which is that it is quit verbose. Second it evaluates the same properties over and over again. In order to reduce the double work we could introduce some extra local variables and create a separete if statement for every step, this will make it even more verbose than it is allready.&lt;/p&gt;&lt;p&gt;Of course C# 3.0 has a lot of new syntax tricks, like extension methods, lambda expression and type inference. I had seen some nice examples (mostly Linq related) but had not been using these features to the max myself. Somehow I got the idea that these features would help me solve the Null checking problem.&lt;/p&gt;&lt;p&gt;I figured that the code I wanted to write was something like the following.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;if (Contract&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .NullSafe(c =&amp;gt; c.Parties)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .NullSafe(p =&amp;gt; p.Client)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .NullSafe(c =&amp;gt; c.Address) != null)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // do something with the adress&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.Writeline(Contract.Parties.Client.Adress.Street);&lt;br /&gt;}&lt;/p&gt;&lt;p&gt;The idea is that all parts of the path are expressed in separate lambda expressions which are chained together. If any link in the chain returns null, the rest of the path is never evaluated and the result of the whole expression will also be null.&lt;br /&gt;All I had to do to make this possible was write one single extension method that would operate on any object, I was quite amazed that&amp;nbsp;I could do this with verry little code that, besides some generic stuff, was&amp;nbsp;actually quite simple.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static TResult NullSafe&amp;lt;T, TResult&amp;gt;(this T target, Func&amp;lt;T, TResult&amp;gt; func)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (target != null)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return func(target);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return default(TResult);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p&gt;&lt;br /&gt;All the generics make it look a lot more complicated than it actually is. This extension method receives two arguments, the first of which is the object it operates on and the second is a generic Func&amp;lt;T, TResult&amp;gt; delegate (the lambda expression). The return type of the extension method is automatically inferred from the return type of the lambda that is passed in. This allows you to call NullSafe again on the result of the first call and use IntelliSense to type p.Client in the second lambda.&lt;br /&gt;The nice thing about extension methods is, besides that they can operate on any existing class without changing it, that they can also operate on Null references without causing a NullReferenceException!! This makes it possible to test for null &lt;strong&gt;inside&lt;/strong&gt; the extension method. If the object it is called on is not null&amp;nbsp;it evaluates the lambda expression and returns the result. Otherwise&amp;nbsp;it returns the default value of the expected return type, for reference types this is Null.&lt;br /&gt;To make life even better I created another overload of NullSafe that was even simpler&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void NullSafe&amp;lt;T&amp;gt;(this T target, Action&amp;lt;T&amp;gt; action)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (target != null)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; action(target);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p&gt;Instead of a generic Func&amp;lt;T, TResult&amp;gt; delegate this one receives a Generic Action&amp;lt;T&amp;gt; delegate and returns void.&lt;br /&gt;This removes the need to retype the whole path inside the if, it actually removes the if altogether.&lt;/p&gt;&lt;p&gt;Contract&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .NullSafe(c =&amp;gt; c.Parties)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .NullSafe(p =&amp;gt; p.Client)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .NullSafe(c =&amp;gt; c.Address)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .NullSafe(a =&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // do something with the adress&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.Writeline(a.Street);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;/p&gt;&lt;p&gt;To bad for me, my current project is still on C# 2.0 so I&amp;#39;ll keep doing it old school style :-(&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Exercise&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;If you would also like some practice with C# 3.0, I would like challenge you to write something that allows me to do the same thing for IEnumerable&amp;lt;T&amp;gt;, I would like to write something like the following &lt;/p&gt;&lt;p&gt;foreach (Order order in Contract&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .NullSafe(c =&amp;gt; c.Parties)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .NullSafe(p =&amp;gt; p.Client)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .NullSafe(c =&amp;gt; c.Orders)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // do something with order&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.Writeline(o.Amount);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p&gt;Where Client.Orders is of type IEnumerable&amp;lt;Order&amp;gt;. The idea is that I want to iterate every Order in Contract.Parties.Client.Orders. If at any stage in the path (including the Orders collection) we encounter a Null reference we will just loop an empty enumerator and do nothing.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.infosupport.com/aggbug.aspx?PostID=13481" width="1" height="1"&gt;</content><author><name>frankb</name><uri>http://blogs.infosupport.com/members/frankb/default.aspx</uri></author><category term=".Net" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/.Net/default.aspx" /><category term="C#" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/C_2300_/default.aspx" /><category term="Lambda expressions" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Lambda+expressions/default.aspx" /><category term="Null reference" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Null+reference/default.aspx" /><category term="C# 3.0" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/C_2300_+3.0/default.aspx" /><category term="Extension methods" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Extension+methods/default.aspx" /></entry><entry><title>Will our current programming model survive multi-core hardware?</title><link rel="alternate" type="text/html" href="/blogs/frankb/archive/2007/07/29/Will-multi_2D00_core-processing-change-the-way-we-write-software_3F00_.aspx" /><id>/blogs/frankb/archive/2007/07/29/Will-multi_2D00_core-processing-change-the-way-we-write-software_3F00_.aspx</id><published>2007-07-29T12:37:00Z</published><updated>2007-07-29T12:37:00Z</updated><content type="html">&lt;p&gt;&lt;span style="font-size:10pt;font-family:'Arial','sans-serif';"&gt;For years the innovations of CPU&amp;#39;s have been focused on increasing the speed at which a single sequence of instructions gets executed (mainly by increasing the clock frequency). In the last years this trend is changing from increasing sequential execution speed to parallel execution (multi CPU / hyper-threading / multi core). If this trend continues the number of processor cores will no longer be measured in dual or quad, but in K, M or G.&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:10pt;font-family:'Arial','sans-serif';"&gt;Today&amp;#39;s popular programming languages (both OO as procedural) encourage (or force) us to write software as a sequence of statements that get executed in a specific order. This paradigm is called an execution thread and it maps very nicely on the traditional CPU model which has a single instruction pointer. Executing tasks in parallel is made possible by calling a system API that creates a new execution thread. This is more a platform than a language feature, which requires even more system API&amp;#39;s to do the required locking and synchronization.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:10pt;font-family:'Arial','sans-serif';"&gt;Generally multi threading is considered an advanced and error prone&amp;nbsp;feature. This is why it is generally used only in situations where parallel execution is an explicit requirement, like in server processes to handle multiple client requests and in GUI applications to allow background processing while the UI keeps handling user events. In situations where tasks &lt;strong&gt;&lt;span style="font-family:'Arial','sans-serif';"&gt;could&lt;/span&gt;&lt;/strong&gt; be executed parallel, but there is no direct need to do so, we usually stick to our traditional sequential programming model. This is a great waste of the hardware&amp;rsquo;s parallel processing ability&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:10pt;font-family:'Arial','sans-serif';"&gt;My personally experience to non-sequential languages is pretty much limited to SQL and XSLT. Maybe it is time to spread the horizon and take a&amp;nbsp;look at some of the modern Functional languages like &lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Arial','sans-serif';"&gt;&lt;a href="http://research.microsoft.com/fsharp/fsharp.aspx"&gt;&lt;span&gt;F#&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Arial','sans-serif';"&gt; &amp;nbsp;and &lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Arial','sans-serif';"&gt;&lt;a href="http://fortress.sunsource.net/" title="Fortress"&gt;&lt;span&gt;Fortress&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Arial','sans-serif';"&gt;. Who is with me?&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.infosupport.com/aggbug.aspx?PostID=12225" width="1" height="1"&gt;</content><author><name>frankb</name><uri>http://blogs.infosupport.com/members/frankb/default.aspx</uri></author><category term="Fortress" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Fortress/default.aspx" /><category term="F#" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/F_2300_/default.aspx" /><category term="multi core" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/multi+core/default.aspx" /><category term="parallel programming" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/parallel+programming/default.aspx" /></entry><entry><title>Lack of support for generic base classes in Windows Forms designer</title><link rel="alternate" type="text/html" href="/blogs/frankb/archive/2007/04/28/Lack-of-support-for-generic-base-classes-in-Windows-Forms-designer.aspx" /><id>/blogs/frankb/archive/2007/04/28/Lack-of-support-for-generic-base-classes-in-Windows-Forms-designer.aspx</id><published>2007-04-28T11:25:00Z</published><updated>2007-04-28T11:25:00Z</updated><content type="html">&lt;p&gt;Just recently having started my first real life .Net 2.0 project I wanted to utilize all the cool new features in my day-to-day job. One of those features is of course the support for Generics in C#. But, as with all cool stuff, getting your hopes up to high can get you disappointed.&lt;/p&gt;&lt;p&gt;In our Windows forms application we use a design pattern in which a UserControl defines a callback interface which is implemented by a mediator. This makes sure we have a loose coupling between the UserControl and the source of the rest of the application.&lt;/p&gt;&lt;p&gt;To avoid having a lot of duplicate code I decided to make a base class that does some of the stuff related to initializing the mediator. While every control will have a different mediator interface this called for generics (sample code is written by hand, so don&amp;#39;t mind the compiler errors).&lt;/p&gt;&lt;p&gt;&lt;br /&gt;class ViewControlBase : UserControl &amp;lt;TMediator&amp;gt;&lt;br /&gt;&amp;nbsp;where TMediator : IMediator&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;TMediator _mediator;&lt;/p&gt;&lt;p&gt;&amp;nbsp;protected TMediator Mediator&lt;br /&gt;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;get { return _mediator; }&lt;br /&gt;&amp;nbsp;}&lt;/p&gt;&lt;p&gt;&amp;nbsp;public InitMediator(IMediator mediator)&lt;br /&gt;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;_mediator = (TMediator) mediator;&lt;br /&gt;&amp;nbsp;&amp;nbsp;// some more generic stuff&lt;br /&gt;&amp;nbsp;}&lt;br /&gt;}&lt;/p&gt;&lt;p&gt;Now the viewControls are derrived from ViewControlBase like this&lt;/p&gt;&lt;p&gt;class CustomerView : ViewControlBase&amp;lt;ICustomerMediator&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;// ... Use typed access to the Mediator property&lt;br /&gt;}&lt;/p&gt;&lt;p&gt;By using a generic base I was able to have Typed access to the Mediator in all the derived classes, and I avoided writing the initialization code over and over again. Hitting F5 proved all worked just as expected.&lt;/p&gt;&lt;p&gt;But when I opened one of the ViewControls in the designer the designer was filled with an error saying something was wrong. Usually this means that you have to rebuild the solution, clean up, rebuild some more and eventually it works. Not is time :-( &lt;/p&gt;&lt;p&gt;The error stated that the class CustomerView was not &amp;#39;Designable&amp;#39; because it was not able to create an instance of ViewControlBase. That is true, it is however possible to create an instance of ViewControllerBase&amp;lt;TMediator&amp;gt;&lt;/p&gt;&lt;p&gt;I found a work-around (or a dirty Hack) by creating an empty dummy class like this&lt;/p&gt;&lt;p&gt;class CustomerView : CustomerViewDummy&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;//...&lt;br /&gt;}&lt;/p&gt;&lt;p&gt;class CustomerViewDummy: ViewControlBase&amp;lt;ICustomerMediator&amp;gt;&lt;br /&gt;{}&lt;/p&gt;&lt;p&gt;Now CustomerView is no longer (directly) derived from a generic class the designer stops complaining and shows the control in design view as expected. Since I didn&amp;#39;t really like this solution I went back to old-school and duplicated the same code in every control (Bummer!)&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.infosupport.com/aggbug.aspx?PostID=12088" width="1" height="1"&gt;</content><author><name>frankb</name><uri>http://blogs.infosupport.com/members/frankb/default.aspx</uri></author><category term="C#" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/C_2300_/default.aspx" /><category term="Generics" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Generics/default.aspx" /><category term="Designer" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Designer/default.aspx" /></entry><entry><title>Timezone aware DateTime stucture in .Net 2.0 ?</title><link rel="alternate" type="text/html" href="/blogs/frankb/archive/2007/03/22/Timezone-aware-DateTime-stucture-in-.Net-2.0.aspx" /><id>/blogs/frankb/archive/2007/03/22/Timezone-aware-DateTime-stucture-in-.Net-2.0.aspx</id><published>2007-03-22T20:00:00Z</published><updated>2007-03-22T20:00:00Z</updated><content type="html">&lt;p&gt;&lt;span style="font-size:10pt;font-family:'Arial','sans-serif';"&gt;The last year and a half I have been working on&amp;nbsp;a project based on .Net framework 1.1, that (among other things) involved handling multiple time zones. If you have ever worked on such a project you either did it wrong without knowing or you have spent a whole lot of time getting it nearly 100% right. The biggest problems are the XML serialization that always assumes en DateTime represents local time, and the lack of a specific Date-only data type which is time zone independent. When it comes to handling DateTimes in different time zones .Net framework 1.1 just doesn&amp;#39;t cut it.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:10pt;font-family:'Arial','sans-serif';"&gt;Framework 2.0 brings partly good news, only last week I happened to &amp;#39;intellisense&amp;#39; over a new property on the DateTime structure; DateTime.Kind. It seems that Microsoft has found a use for 2 unused bits in the 64 bits that hold up the data for a DateTime structure. These 2 bits were not enough to store the actual UTC offset.&amp;nbsp;Instead they choose for three possible values, Local, UTC and Unknown. This is a big improvement because it solves the incorrect XML serialization problem of UTC DateTimes. &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:10pt;font-family:'Arial','sans-serif';"&gt;The lack of the actual UTC offset in the DateTime structure still has the downside of not being able to handle time zones other than UTC or your own (the one configured in the regional settings) without storing the offset in a separate field and doing your own transformations. Of course the DateTime being a structure which cannot be inherited from doesn&amp;#39;t really help to create a generic solution for this one. Second it causes an ambiguity in the hour in the fall when the clock shifts to daylight saving time. This can cause an UTC time being converted to local and back to UTC not to be the same as the original. Last time this happened some of our unit tests broke because the daily build cycle runs exactly within this hour.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.infosupport.com/aggbug.aspx?PostID=11895" width="1" height="1"&gt;</content><author><name>frankb</name><uri>http://blogs.infosupport.com/members/frankb/default.aspx</uri></author><category term=".Net" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/.Net/default.aspx" /><category term="DateTime" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/DateTime/default.aspx" /><category term="Time zone" scheme="http://blogs.infosupport.com/blogs/frankb/archive/tags/Time+zone/default.aspx" /></entry></feed>