- 浏览: 404265 次
- 性别:
- 来自: 上海
-
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
Let first take a prediction, what is the result of the following code.
public static void ClosureForeachWithLambda() { var action = new List<Action>(); var list = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; foreach (var i in list) { action.Add(() => Console.WriteLine(i)); } foreach (var i in action) { i(); } }
It is not 1..9 printed each once, instead it is the 9 printed 9 times. Not belieing, huh, trying it yourself.
So, let me post a complete code with comments to tell why this is happening. Also, in my code below, you will see a fix to the bug in .net 4. 0
public static void Main(string[] args) { ClosureForeachWithLambdaGotha(); } public static void ClosureForeachWithLambda() { // the reason why this is a potential pitfall is because // closure is not creating a copy, not copying the value, but instead it is // the variable itself. // but inside theo foreach loop, // it is loop variable that is bound to and the bound is updated in each loop // so at last, you will see that it is '9' that is printed 9 times var action = new List<Action>(); var list = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; foreach (var i in list) { action.Add(() => Console.WriteLine(i)); } foreach (var i in action) { i(); } } public static void ClosureForeachWithLambdaGotha() { // a fix is to explicitly create a local variable, // by which it is the variable in new scope that is closured // here shows how: // // fortunately thi is going to be fixed in .net 4.5 // however, the bad news is that since 4.5 is going to be a inplace change. // so that means it is going to be a breaking change. var action = new List<Action>(); var list = new [] { 1,2 ,3, 4, 5, 6, 7, 8, 9}; foreach (var i in list) { var copy = i; action.Add(() => Console.WriteLine(copy)); } foreach (var i in action) { i(); } }
As explained in the post : Lifting Foreach, Breaking change in Visual Studio 2012. It is going to be a breaking change, as the .net 4.5 is going to be a in place patch .
发表评论
-
wpf - example to enhance ComboBox for AutoComplete
2014-09-19 15:56 1987first let’s see an example ... -
Investigate and troubleshoot possible memory leak issue of .NET application
2014-07-31 10:42 0Hi All, I would like to sh ... -
C# – CoerceValueCallback合并、替换元数据值
2013-08-05 21:59 1940Topic: C# – CoerceValueCallbac ... -
wpf – ListView交替背景色
2013-07-02 20:56 6569Wpf – Alternate background col ... -
C# - 简单介绍TaskScheduler
2013-06-29 17:18 12070标题: C# - 简单介绍TaskSchedulerTit ... -
c# - Get enum from enum attribute
2013-06-27 21:32 1267DescriptionAttribute gives the ... -
C# - PInvoke, gotchas on the RegisterClassEx and the CreateWindowEx
2013-06-24 13:49 2588I get an exception message li ... -
c# - Use PInvoke to create simple win32 Application
2013-06-24 11:59 10977In this post, .net platform h ... -
c# - Linq's Select method as the Map function
2013-06-19 18:47 1304If you comes from a functiona ... -
c# - Tips of Linq expression Any to determine if a collection is Empty
2013-06-19 18:29 957When you are @ the linq expres ... -
myth buster - typeof accepting array of types not acceptable
2013-06-19 17:17 827I have seen from some book whe ... -
windows - trying to create WIN32 application with PInvoke
2013-06-19 14:34 0While it is stupid to do such ... -
WPF - Setting foreground color of Entire window
2013-06-13 16:00 1937You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5348As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1204Recently I was trying to debug ... -
c# - P/Invoke, DllImport, Marshal Structures and Type conversions
2013-06-05 15:25 1726P/Invoke as in the following q ... -
c# - A study on the NativeWindow - encapsulate window handle and procedure
2013-06-05 14:40 6110NativeWindow gives you a way t ... -
WCF - Notify server when client connects
2013-06-03 18:19 1236It is sometimes very importan ... -
wcf - Debug to enable Server exception in Fault message
2013-06-03 15:47 1112WCF will be able to send back ... -
c# - determine if a type/object is serialzable
2013-05-30 16:35 877In WCF, primitives type are s ...
评论