- 浏览: 402834 次
- 性别:
- 来自: 上海
-
文章分类
- 全部博客 (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 1982first 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 1934Topic: C# – CoerceValueCallbac ... -
wpf – ListView交替背景色
2013-07-02 20:56 6563Wpf – Alternate background col ... -
C# - 简单介绍TaskScheduler
2013-06-29 17:18 12062标题: C# - 简单介绍TaskSchedulerTit ... -
c# - Get enum from enum attribute
2013-06-27 21:32 1259DescriptionAttribute gives the ... -
C# - PInvoke, gotchas on the RegisterClassEx and the CreateWindowEx
2013-06-24 13:49 2582I get an exception message li ... -
c# - Use PInvoke to create simple win32 Application
2013-06-24 11:59 10969In this post, .net platform h ... -
c# - Linq's Select method as the Map function
2013-06-19 18:47 1301If you comes from a functiona ... -
c# - Tips of Linq expression Any to determine if a collection is Empty
2013-06-19 18:29 951When you are @ the linq expres ... -
myth buster - typeof accepting array of types not acceptable
2013-06-19 17:17 824I 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 1932You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5345As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1196Recently I was trying to debug ... -
c# - P/Invoke, DllImport, Marshal Structures and Type conversions
2013-06-05 15:25 1723P/Invoke as in the following q ... -
c# - A study on the NativeWindow - encapsulate window handle and procedure
2013-06-05 14:40 6106NativeWindow gives you a way t ... -
WCF - Notify server when client connects
2013-06-03 18:19 1232It is sometimes very importan ... -
wcf - Debug to enable Server exception in Fault message
2013-06-03 15:47 1105WCF will be able to send back ... -
c# - determine if a type/object is serialzable
2013-05-30 16:35 873In WCF, primitives type are s ...
相关推荐
在C#编程语言中,`foreach`循环是一个非常重要的控制流结构,用于遍历集合、数组或枚举类型的元素。这个`c#Foreach.rar`压缩包包含了一个名为`foreach.txt`的文本文件,很可能是对`foreach`用法的简要说明。在这里,...
C#中Foreach详细用法 Foreach语句是C#语言中的一种循环语句,它提供了一个简洁高效的方式来遍历数组、集合、列表等数据结构。在本文中,我们将详细介绍Foreach语句的用法和优点,并与传统的for循环语句进行比较。 ...
Lambda表达式是C#编程语言中的一个重要特性,它在处理函数式编程和LINQ查询时尤其有用。Lambda表达式提供了一种简洁的方式来定义匿名函数,使得代码更加紧凑和易读。下面我们将深入探讨C# Lambda表达式的概念、语法...
在foreach中删除元素时,每一次删除都会导致集合的大小和元素索引值发生变化,从而导致在foreach中删除元素时会抛出异常。 集合已修改;可能无法执行枚举操作。 方法一:采用for循环,并且从尾到头遍历 如果...
Csharp Foreach用法 可以对比for的区别
C#并发实战记录之Parallel.ForEach使用 本篇文章主要介绍了C#并发实战记录之Parallel.ForEach使用的相关知识点。通过示例代码,详细介绍了Parallel.ForEach的使用方法和优化技巧。 一、使用Parallel.ForEach优化...
Java 8 是一个重要的Java平台版本,因为它引入了许多新特性,其中最显著的就是Lambda表达式。Lambda表达式是函数式编程的关键元素,它允许我们以更简洁、更易读的方式编写代码,特别是在处理集合和并发任务时。在这...
Lambda表达式是C#中的一个功能强大的特性,常用于简化代码,特别是在LINQ(Language Integrated Query)查询中。本篇文章将深入探讨如何利用C#中的Lambda表达式和EF框架来操作SQL数据库。 首先,要使用EF,你需要在...
在C#编程语言中,`foreach`循环是一种用于遍历集合、数组或其他可迭代对象的便利语法。在本文中,我们将深入探讨`foreach`循环的工作原理、如何使用它以及一些实用案例,这些案例都是在Visual Studio 2019环境下编写...
在C#编程语言中,`foreach`循环是一个非常重要的语法结构,它用于遍历集合、数组或其他可迭代对象中的元素。这个压缩包文件"foreach测试.rar"可能包含了一些示例代码,用于展示`foreach`循环的工作原理和最佳实践。...
本文将详细给大家关于C#中foreach循环对比for循环的优势和劣势,下面话不多说了,来一起看看详细的介绍吧。 一、foreach循环的优势 C#支持foreach关键字,foreach在处理集合和数组相对于for存在以下几个优势: 1、...
C# foreach原理 foreach语句是C#中用于遍历数组、集合或其他实现了IEnumerable接口的数据结构的一种便捷方式。在C#中,foreach语句隐藏了迭代器的复杂性,让开发者可以轻松地对集合中的每个元素执行操作,而无需...
在.NET编程环境中,`Parallel.ForEach`是一个非常有用的并行处理工具,它允许开发者将数据集中的每个元素在多个线程上并行处理,以提高应用程序的执行效率。然而,正如标题所指出的,`Parallel.ForEach`可能会遇到...
在C#编程语言中,`foreach`语句用于遍历集合、数组或其他可迭代对象中的元素,这在处理数据集合时非常常见。然而,在某些情况下,我们可能希望在遍历过程中提前终止循环,这时就可以使用`break`语句。本文将深入探讨...
在C#编程中,`foreach`循环是一种常用的迭代器,用于遍历集合中的元素,如列表、数组等。然而,当尝试使用`foreach`遍历一个值为`null`的集合时,程序会抛出一个`NullReferenceException`异常。为避免这种情况,我们...
C#中foreach实现原理详解 C#中foreach的实现原理是基于可枚举类型和枚举器的概念。可枚举类型是指实现了IEnumerable接口的类,例如List、ArrayList等。这些类都可以用foreach进行遍历。 在C#中,foreach语句的执行...
`for` 和 `foreach` 循环在C#中被广泛使用,它们各自有特定的用途和效率特点。本篇将深入探讨这两种循环结构,以及它们在执行大量遍历操作时的性能差异。 `for` 循环是一种灵活的循环结构,适用于需要知道循环次数...
本文将深入探讨`for`和`foreach`在C#中的效率差异,以及如何根据具体场景选择合适的循环方式。 首先,`for`循环是一种更基础的迭代结构,它允许程序员直接控制循环变量的初始化、条件检查和递增/递减过程。`for`...
C#中的`foreach`遍历是编程中常用的操作,它简化了对集合、数组和列表等数据结构的迭代。在C#中,`foreach`循环主要用于遍历实现了`IEnumerable`接口的对象,包括数组、列表(`List<T>`)、字典(`Dictionary, ...