C# 表达式Lambda与语句Lambda
从根本上讲,单行表达式lambda和语句lambda之间有什么区别吗?以以下代码为例:C# 表达式Lambda与语句Lambda,c#,lambda,C#,Lambda,从根本上讲,单行表达式lambda和语句lambda之间有什么区别吗?以以下代码为例: private delegate void MyDelegate(); protected static void Main() { MyDelegate myDelegate1 = () => Console.WriteLine("Test 1"); MyDelegate myDelegate2 = () => { Console.WriteLine("Test 2"); };
private delegate void MyDelegate();
protected static void Main()
{
MyDelegate myDelegate1 = () => Console.WriteLine("Test 1");
MyDelegate myDelegate2 = () => { Console.WriteLine("Test 2"); };
myDelegate1();
myDelegate2();
Console.ReadKey();
}
public static double AreaOfTriangle(double itsbase, double itsheight)
{
return itsbase * itsheight / 2;
}
虽然我更喜欢第一个,因为我发现括号很难看,但这两者之间有什么不同吗(除了关于多行语句需要括号的明显部分)两个是相同的-第一个是到第二个,两个都将编译到相同的IL。两个是相同的-第一个是到第二个,两个都将编译到相同的IL。否,本例中没有区别。如果lambda的主体只是一个表达式,则可以删除括号。但是,一旦lambda包含多个表达式,如下所示:
MyDelegate myDelegate2 = () => {
Console.WriteLine("Test 2");
Console.WriteLine("Test 2");
};
括号是必需的。否,此示例中没有区别。如果lambda的主体只是一个表达式,则可以删除括号。但是,一旦lambda包含多个表达式,如下所示:
MyDelegate myDelegate2 = () => {
Console.WriteLine("Test 2");
Console.WriteLine("Test 2");
};
括号是必需的。多语句lambda需要语句lambda。此外,LINQ到SQL等表达式提供程序不支持语句lambda。在.NET4.0之前,.NETFramework不支持语句表达式树。这是在4.0中添加的,但据我所知,没有提供商使用它
Action myDelegate1 = () => Console.WriteLine("Test 1");
Expression<Action> myExpression = () => { Console.WriteLine("Test 2") }; //compile error unless you remove the { }
myDelegate1();
Action myDelegate2 = myExpression.Compile();
myDelegate2();
Action myDelegate1=()=>Console.WriteLine(“测试1”);
表达式myExpression=()=>{Console.WriteLine(“test2”)}//除非删除{},否则编译错误
myDelegate1();
Action myDelegate2=myExpression.Compile();
myDelegate2();
否则它们是相同的。多语句lambda需要语句lambda。此外,LINQ到SQL等表达式提供程序不支持语句lambda。在.NET4.0之前,.NETFramework不支持语句表达式树。这是在4.0中添加的,但据我所知,没有提供商使用它
Action myDelegate1 = () => Console.WriteLine("Test 1");
Expression<Action> myExpression = () => { Console.WriteLine("Test 2") }; //compile error unless you remove the { }
myDelegate1();
Action myDelegate2 = myExpression.Compile();
myDelegate2();
Action myDelegate1=()=>Console.WriteLine(“测试1”);
表达式myExpression=()=>{Console.WriteLine(“test2”)}//除非删除{},否则编译错误
myDelegate1();
Action myDelegate2=myExpression.Compile();
myDelegate2();
否则它们是一样的。我个人更喜欢Lambda表达式。表达式具有语句不具有的值 我认为以下链接可以帮助您:
我个人更喜欢Lambda表达式。表达式具有语句不具有的值 我认为以下链接可以帮助您:
欢迎营救!反汇编代码如下所示:
private static void Main(string[] args)
{
MyDelegate myDelegate1 = delegate {
Console.WriteLine("Test 1");
};
MyDelegate myDelegate2 = delegate {
Console.WriteLine("Test 2");
};
myDelegate1();
myDelegate2();
Console.ReadKey();
}
因此,两者之间没有真正的区别。开心点。欢迎营救!反汇编代码如下所示:
private static void Main(string[] args)
{
MyDelegate myDelegate1 = delegate {
Console.WriteLine("Test 1");
};
MyDelegate myDelegate2 = delegate {
Console.WriteLine("Test 2");
};
myDelegate1();
myDelegate2();
Console.ReadKey();
}
因此,两者之间没有真正的区别。开心点。如果委托返回一个值,则必须在语句lambda中返回
,如下所示
Func<int, int, bool> foo = (x, y) => { return x == y; };
Func<int, int, bool> goo = (x, y) => x == y;
Func-foo=(x,y)=>{return x==y;};
Func-goo=(x,y)=>x==y;
如果委托返回一个值,则语句lambda中必须返回值,如下所示
Func<int, int, bool> foo = (x, y) => { return x == y; };
Func<int, int, bool> goo = (x, y) => x == y;
Func-foo=(x,y)=>{return x==y;};
Func-goo=(x,y)=>x==y;
与OP示例相同,但在C#6.0之后,允许使用相同的表达式语法在类中定义正常的非lambda方法。例如:
private delegate void MyDelegate();
protected static void Main()
{
MyDelegate myDelegate1 = () => Console.WriteLine("Test 1");
MyDelegate myDelegate2 = () => { Console.WriteLine("Test 2"); };
myDelegate1();
myDelegate2();
Console.ReadKey();
}
public static double AreaOfTriangle(double itsbase, double itsheight)
{
return itsbase * itsheight / 2;
}
只有当方法可以转换为单个表达式时,才能编写上述代码段。简而言之,它可以用于表达式lambda语法,但不能用于语句lambda语法
public static double
AreaOfTrianglex(double itsbase, double itsheight) => itsbase * itsheight / 2;
OP示例也是如此,但在C#6.0之后,允许使用相同的表达式语法在类中定义普通的非lambda方法。例如:
private delegate void MyDelegate();
protected static void Main()
{
MyDelegate myDelegate1 = () => Console.WriteLine("Test 1");
MyDelegate myDelegate2 = () => { Console.WriteLine("Test 2"); };
myDelegate1();
myDelegate2();
Console.ReadKey();
}
public static double AreaOfTriangle(double itsbase, double itsheight)
{
return itsbase * itsheight / 2;
}
只有当方法可以转换为单个表达式时,才能编写上述代码段。简而言之,它可以用于表达式lambda语法,但不能用于语句lambda语法
public static double
AreaOfTrianglex(double itsbase, double itsheight) => itsbase * itsheight / 2;
从文档()中:
语句lambda与匿名方法一样,不能用于创建表达式树。来自文档():
语句lambda与匿名方法一样,不能用于创建表达式树。除了多个语句之外,还存在return关键字+1除了多个语句之外,return关键字的存在也是另一个事实+1.