From page 133 of "C# 4.0 in a Nutshell" by Joseph Albahari and Ben Albahari:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Whatever.Tests
{
[TestClass]
public class ClosureTest
{
[TestMethod]
public void Test()
{
string whatever = "";
Action[] actions = new Action[3];
for (int i = 0; i < 3; i++)
{
actions[i] = () => whatever = whatever + i;
}
foreach (Action a in actions) a();
Assert.AreEqual(whatever,"333");
}
}
}
No comments:
Post a Comment