Created
May 21, 2012 16:21
-
-
Save kamukiriri/2763121 to your computer and use it in GitHub Desktop.
ラムダ式サンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace BatchConsole | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var list = new List<Action>(); | |
var array = new[] { 1,2,3 }; | |
foreach (var val in array) | |
{ | |
Action act = () => Console.WriteLine(val); | |
list.Add(act); | |
} | |
foreach (var act in list) | |
{ | |
act(); //常に 3 が出力される | |
} | |
Console.ReadKey(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace BatchConsole | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var list = new List<Action>(); | |
var array = new[] { 1, 2, 3 }; | |
foreach (var temp in array) | |
{ | |
var val = temp; | |
Action act = () => Console.WriteLine(val); | |
list.Add(act); | |
} | |
foreach (var act in list) | |
{ | |
act(); //1,2,3と出力される | |
} | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment