Csharp/C Sharp/LINQ/Lambda

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

A lambda expression can reference the local variables and parameters of the method in which it"s defined.

<source lang="csharp">

using System; delegate int NumericSequence(); class Test {

   static void Main() {
       int seed = 0;
       NumericSequence natural = () => seed++;
       Console.WriteLine(natural());
       Console.WriteLine(natural());
   }

}

</source>


A lambda expression has the following BNF form: (parameters) => expression-or-statement-block

<source lang="csharp"> using System; class Test {

   static void Main() {
       Func<int, int> square = x => x * x;
       Console.WriteLine(square(3));     // 9
   }

}

</source>


A local variable instantiated within a lambda expression is unique per invocation of the delegate instance.

<source lang="csharp"> using System; delegate int NumericSequence(); class Test {

   static NumericSequence Natural() {
       return () => { int seed = 0; return seed++; };
   }
   static void Main() {
       NumericSequence natural = Natural();
       Console.WriteLine(natural());
       Console.WriteLine(natural());
   }

}

</source>


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

<source lang="csharp"> 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 + "|");   
   }

}

</source>


Lambda expression used to declare a delegate

<source lang="csharp"> using System; using System.Linq; using System.Linq.Expressions; class Program {

   static void Main(string[] args) {
       Func<int, bool> isOddDelegate = i => (i & 1) == 1;
       for (int i = 0; i < 10; i++) {
           if (isOddDelegate(i))
               Console.WriteLine(i + " is odd");
           else
               Console.WriteLine(i + " is even");
       }
   }

}

</source>


Lambda expression used to declare an expression tree

<source lang="csharp"> using System; using System.Linq; using System.Linq.Expressions; class Program {

   static void Main(string[] args) {
       Expression<Func<int, bool>> isOddExpression = i => (i & 1) == 1;
       ParameterExpression param = Expression.Parameter(typeof(int), "i");
       Expression<Func<int, bool>> isOdd =
           Expression.Lambda<Func<int, bool>>(
           Expression.Equal(
             Expression.And(
               param,
               Expression.Constant(1, typeof(int))),
             Expression.Constant(1, typeof(int))),
           new ParameterExpression[] { param });
       Func<int, bool> isOddCompiledExpression = isOddExpression.rupile();
       for (int i = 0; i < 10; i++) {
           if (isOddCompiledExpression(i))
               Console.WriteLine(i + " is odd");
           else
               Console.WriteLine(i + " is even");
       }
   }

}

</source>


return a lambda function

<source lang="csharp"> using System; delegate int NumericSequence(); class Test {

   static NumericSequence Natural() {
       int seed = 0;
       return () => seed++;
   }
   static void Main() {
       NumericSequence natural = Natural();
       Console.WriteLine(natural());
       Console.WriteLine(natural());
   }

}

</source>


square is assigned the lambda expression x = > x * x:

<source lang="csharp"> using System; delegate int Transformer(int i); class Test {

   static void Main() {
       Transformer square = x => x * x;
       Console.WriteLine(square(3));    // 9
   }

}

</source>