代码改变世界

.NET 匿名方法的BUG,请专家解答

  钟铧若岩  阅读(3991)  评论(75编辑  收藏  举报

匿名方法是.NET 3.5之后的一个好东东,很多人使用,但是我在最近的工作当中发现了一个问题. 请专家解答

 

复制代码
 1             //list里存放10个数字
 2             List<int> list = new List<int>(10);
 3             for (int i = 0; i < 10; i++)
 4             {
 5                 list.Add(i);
 6             }
 7 
 8             //10个数字,分成10组,其实每组就一个元素,每组的元素是不相同的
 9             Dictionary<int, List<int>> dict = new Dictionary<int, List<int>>();
10             for (int i = 0; i < 10; i++)
11             {
12                 int k = i % 10;
13                 if (dict.ContainsKey(k))
14                 {
15                     dict[k].Add(i);
16                 }
17                 else
18                 {
19                     dict[k] = new List<int>();
20                     dict[k].Add(i);
21                 }
22             }
复制代码

 

接下来,我们先采用非匿名方法,实现打印每个组里的元素,代码如下

复制代码
 1             using (Dictionary<int, List<int>>.Enumerator enumerator = dict.GetEnumerator())
 2             {
 3                 KeyValuePair<int, List<int>> keyValue;
 4                 while (enumerator.MoveNext())
 5                 {
 6                     keyValue = enumerator.Current;
 7    
 8                     System.Threading.Thread thread = new System.Threading.Thread(Display);
 9                     thread.Start(keyValue.Value);
10                 }
11             }
12 
13    public static void Display(object o)
14         {
15             List<int> list = o as List<int>;
16             foreach (var item in list)
17             {
18                 Console.WriteLine(item.ToString());
19             }
20         }
复制代码

 

输出的结果是: 

0

1

2

3

4

5

6

7

8

9

没有问题!!! 一切OK

 

好,我们换一种方式为实现输出各组的元素,采用匿名委托的方式.

复制代码
 1           using (Dictionary<int, List<int>>.Enumerator enumerator = dict.GetEnumerator())
 2             {
 3                 KeyValuePair<int, List<int>> keyValue;
 4                 while (enumerator.MoveNext())
 5                 {
 6                     keyValue = enumerator.Current;
 7                     System.Threading.Thread thread = new System.Threading.Thread(delegate()
 8                     {
 9                         foreach (var item in keyValue.Value)
10                         {
11                             Console.WriteLine(item.ToString());
12                         }
13                     }
14                   );
15                     thread.Start();
16                 }
17             }
复制代码

采用上面的代码,输出的结果不定,而且会出现重复的数据,结果可能如下

3

3

3

4

4

4

5

5

7

9

 

请专家解答一下,我一直没有找到原因!

 

这个号是09年注册的,可是博文只写了几个,感谢大家的热情回复!

此题可以结了。请大家看回复之后,如果有补充的地方再回复! 

 

换成下面这个方式就可以解决了,谢谢10楼的伙伴 

复制代码
using (Dictionary<int, List<int>>.Enumerator enumerator = dict.GetEnumerator())
{
KeyValuePair<int, List<int>> keyValue;
while (enumerator.MoveNext())
{
keyValue = enumerator.Current;
System.Threading.Thread thread = new System.Threading.Thread(delegate(object o)
{
List<int> ls = o as List<int>;
foreach (var item in ls)
{
Console.WriteLine(item.ToString());
}
});
thread.Start(keyValue.Value);
}
}
复制代码

 

相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
点击右上角即可分享
微信分享提示