Skip to main content


Showing posts from December, 2012

Top posts from 2012 – Roundup Post

I would like to wish all my readers an amazing 2013 filled with joy and enthusiasm. Here are some of the top 2012 posts/publicationsChanging times for web developers Top 500 MSDN links from Stackoverflow using Apache Hadoop, Azure and C# Hadoop Jobs in C# Signal Wire Points C# developers should know about WinRT to TypeScript for C# developers MSDN links from Stack Overflow hosting Web APIshttp://www.…

A quick note on Closing lambda loop variables in C# 5.0 - Breaking Changes in C# 5.0 compiler

This post is regarding a breaking change in C# 5.0 related to using variables in lambdas with in a loop. For example, prior to C# 5.0 compiler, the following code //** Code Example 1 ** static void Main(string[] args) { var items = new List<int>() { 1, 2, 3, 4, 5 }; var queue = new List<Func<int>>(); foreach (var item in items) queue.Add(() => item); foreach (var q in queue) Console.WriteLine(q()); Console.Read(); } will produce the following output This is because prior the C# 5.0, the compiler closes/captures the reference of the lambda/loop variable than its actual value. And hence the value defaults to the current value at the time of invocation (here, when invoking the q() method) instead of the value at the time of using the variable in the expression (when adding it to the queue). So, earlier, the compiler forced you to declare a local vari…