lambda expression和lambda methods
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestMod
{
public class Program
{
static void Main(string[] args)
{
/*
* Lambda expressions look just like lambda methods-the syntax is idential-
* but during compilation they aren't converted into anonymous delegates.
* Instread, they're embedded in the assembly as data, not code,
* called an abstract syntax tree(AST). Here's an example:
* */
//Func<int, int, int> add = (a , b) => { /* a++; b++; */ return a + b; }; //法1:匿名委托
Func<int, int, int> add = (a, b) => a + b; //法2:简便lambda method
int ress = add(1, 2);
Console.WriteLine(ress);
//this is a Lambda expression, and is compiled to *data* (AST)
System.Linq.Expressions.Expression<Func<int, int, int> > express = (a, b) => a + b;
int result = express.Compile()(3, 4);
Console.WriteLine(result);
//-------------------lambda表达式示例1 begin--------------------
//string[] names = {"Dannis wu","Spark John","jj","gugu" };
//IEnumerable<string> eStrs= names.Where(s => s.EndsWith("u"));
//foreach (var item in eStrs)
//{
// Console.WriteLine(item);
//}
//-------------------lambda表达式示例1 end--------------------
//Console.WriteLine(-99 % -100); //结果为-99, (n%p)模的结果正负,与n有关,与p无关
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestMod
{
public class Program
{
static void Main(string[] args)
{
/*
* Lambda expressions look just like lambda methods-the syntax is idential-
* but during compilation they aren't converted into anonymous delegates.
* Instread, they're embedded in the assembly as data, not code,
* called an abstract syntax tree(AST). Here's an example:
* */
//Func<int, int, int> add = (a , b) => { /* a++; b++; */ return a + b; }; //法1:匿名委托
Func<int, int, int> add = (a, b) => a + b; //法2:简便lambda method
int ress = add(1, 2);
Console.WriteLine(ress);
//this is a Lambda expression, and is compiled to *data* (AST)
System.Linq.Expressions.Expression<Func<int, int, int> > express = (a, b) => a + b;
int result = express.Compile()(3, 4);
Console.WriteLine(result);
//-------------------lambda表达式示例1 begin--------------------
//string[] names = {"Dannis wu","Spark John","jj","gugu" };
//IEnumerable<string> eStrs= names.Where(s => s.EndsWith("u"));
//foreach (var item in eStrs)
//{
// Console.WriteLine(item);
//}
//-------------------lambda表达式示例1 end--------------------
//Console.WriteLine(-99 % -100); //结果为-99, (n%p)模的结果正负,与n有关,与p无关
Console.ReadKey();
}
}
}