Csharp/C Sharp/LINQ/Lambda
Содержание
- 1 A lambda expression can reference the local variables and parameters of the method in which it"s defined.
- 2 A lambda expression has the following BNF form: (parameters) => expression-or-statement-block
- 3 A local variable instantiated within a lambda expression is unique per invocation of the delegate instance.
- 4 If your query"s lambda expressions reference local variables, these variables are subject to outer variable semantics.
- 5 Lambda expression used to declare a delegate
- 6 Lambda expression used to declare an expression tree
- 7 return a lambda function
- 8 square is assigned the lambda expression x = > x * x:
A lambda expression can reference the local variables and parameters of the method in which it"s defined.
using System;
delegate int NumericSequence();
class Test {
static void Main() {
int seed = 0;
NumericSequence natural = () => seed++;
Console.WriteLine(natural());
Console.WriteLine(natural());
}
}
A lambda expression has the following BNF form: (parameters) => expression-or-statement-block
using System;
class Test {
static void Main() {
Func<int, int> square = x => x * x;
Console.WriteLine(square(3)); // 9
}
}
A local variable instantiated within a lambda expression is unique per invocation of the delegate instance.
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());
}
}
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 + "|");
}
}
Lambda expression used to declare a delegate
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");
}
}
}
Lambda expression used to declare an expression tree
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");
}
}
}
return a lambda function
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());
}
}
square is assigned the lambda expression x = > x * x:
using System;
delegate int Transformer(int i);
class Test {
static void Main() {
Transformer square = x => x * x;
Console.WriteLine(square(3)); // 9
}
}