3 回答

TA貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊
在C#5之前
foreach
Foo f;while(iterator.MoveNext()){ f = iterator.Current; // do something with f}
f
foreach(Foo f in ...) { Foo tmp = f; // do something with tmp}
tmp
static void Main() { int[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; foreach (int i in data) { new Thread(() => Console.WriteLine(i)).Start(); } Console.ReadLine(); }
1 3 4 4 5 7 7 8 9 9
foreach (int i in data) { int j = i; new Thread(() => Console.WriteLine(j)).Start(); }

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
using System;using System.Collections.Generic;class Test{ static void Main() { List<Action> badActions = new List<Action>(); List<Action> goodActions = new List<Action>(); for (int i=0; i < 10; i++) { int copy = i; badActions.Add(() => Console.WriteLine(i)); goodActions.Add(() => Console.WriteLine(copy)); } Console.WriteLine("Bad actions:"); foreach (Action action in badActions) { action(); } Console.WriteLine("Good actions:"); foreach (Action action in goodActions) { action(); } }}
- 3 回答
- 0 關(guān)注
- 871 瀏覽
添加回答
舉報(bào)