Skip to content

Instantly share code, notes, and snippets.

@kamukiriri
Created May 21, 2012 16:21
Show Gist options
  • Save kamukiriri/2763121 to your computer and use it in GitHub Desktop.
Save kamukiriri/2763121 to your computer and use it in GitHub Desktop.
ラムダ式サンプル
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();
}
}
}
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