If you capture a for loop iteration variable in a lambda expression, the value of the variable that the expression uses will be whatever the final value of the iteration variable is when the loop completes.

Capturing the iteration variable in a foreach loop works differently. When you capture the iteration variable in a foreach loop, the lambda expression has a copy of the iteration variable with the value that it had at the time that it was captured.

Lambda expressions were introduced in C# 3.0 and supersede anonymous methods, which were introduced in C# 2.0. There are some differences between lambda expressions and anonymous methods, but lambdas are now the preferred way to write inline code.

You can use a lambda expression anywhere that a delegate instance is expected. This includes passing a lambda expression as a parameter to a method that takes a delegate type.

In the example below, we have a method that has two parameters that are delegate types. It takes a transformer function that accepts and int and returns an int. It also accepts a delegate that reports something about two different int values.

A lambda expression is an unnamed method that appears in-line in the code where it used. It can be used wherever your code expects an instance of a delegate.

Below, the lambda expression takes the place of a method name in assigning a value to a delegate instance. The in-line expression behaves exactly as the method does, returning a string constructed from two input parameters (int and double).

In either case, we can invoke the code (named method or lambda expression) by invoking the delegate instance and passing it the input parameters.