lambda iteration variables
In this chapter you will learn:
lambda and iteration variables
using System;/*from ja v a2s .c o m*/
class Program
{
public static void Main()
{
Action[] actions = new Action[3];
for (int i = 0; i < 3; i++) {
actions[i] = () => Console.WriteLine(i);
}
for (int i = 0; i < 3; i++)
{
actions[i]();
}
}
}
The output:
The fix the problem:
using System;//from ja v a 2 s . c o m
class Program
{
public static void Main()
{
Action[] actions = new Action[3];
int i = 0;
actions[0] = () => Console.Write(i);
i = 1;
actions[1] = () => Console.Write(i);
i = 2;
actions[2] = () => Console.Write(i);
i = 3;
for (i = 0; i < 3; i++)
{
actions[i]();
}
}
}
The output:
Next chapter...
What you will learn in the next chapter:
Home » C# Tutorial » delegate, lambda, event