Skip to main content

Posts

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 developershttp://www.amazedsaint.com/2012/11/changing-times-for-web-developers-6.htmlExtracting Top 500 MSDN links from Stackoverflow using Apache Hadoop, Azure and C#http://www.amazedsaint.com/2012/06/top-500-msdn-links-from-stack-overflow.htmlWriting Hadoop Jobs in C#http://www.codeproject.com/Articles/398563/Analyzing-some-Big-Data-Using-Csharp-Azure-And-ApaIntroducing Signal Wirehttp://www.amazedsaint.com/2012/09/signalwire-magical-plumbing-with-your.html5 Points C# developers should know about WinRThttp://www.amazedsaint.com/2012/09/5-points-developers-should-know-about.htmlIntroduction to TypeScript for C# developershttp://www.amazedsaint.com/2012/10/microsoft-typescript-and-quick.htmlTop MSDN links from Stack Overflowhttp://www.amazedsaint.com/2012/06/top-500-msdn-links-from-stack-overflow.htmlSelf 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…