If your query's lambda expressions reference local variables, these variables are subject to outer variable semantics. : Lambda « LINQ « C# / C Sharp






If your query's lambda expressions reference local variables, these variables are subject to outer variable semantics.

 

using System;
using System.Collections.Generic;
using System.Linq;
public class MainClass {
    public static void Main() {

        int[] numbers = { 1, 2 };

        int factor = 10;
        IEnumerable<int> query = numbers.Select(n => n * factor);

        factor = 20;
        foreach (int n in query) Console.Write(n + "|");   
    }
}

 








Related examples in the same category

1.Lambda expression used to declare a delegate
2.Lambda expression used to declare an expression tree
3.return a lambda function
4.A local variable instantiated within a lambda expression is unique per invocation of the delegate instance.
5.square is assigned the lambda expression x = > x * x:
6.A lambda expression has the following BNF form: (parameters) => expression-or-statement-block
7.A lambda expression can reference the local variables and parameters of the method in which it's defined.