在C#6.0的表达浓郁的函数成员的效率和性能 [英] Expression-bodied function members efficiency and performance in C# 6.0
问题描述
在新的C#6.0,我们可以定义使用lambda表达式的方法和属性。
比如这个属性。
公共字符串名称{{返回首页++尾; }}
现在可以定义如下:
公共字符串名称=>首先++尾;
有关表达式boided函数成员,你可以在这里找到的信息:的http://blogs.msdn.com/b/csharpfaq/archive/2014/二十零分之十一/新功能 - 在C-6.aspx
有谁知道,如果使用新语法的时候有任何开销?它可以减缓(或提高效率)申请或者不要紧?
在新的C#6.0,我们可以定义使用lambda表达式的方法和属性。
块引用>
没有,你不能。您可以使用语法其中的看起来定义方法和属性机构的像一个lambda表达式,因为它使用令牌
=方式>
不过,重要的这样做的不的意思是有涉及委托类型。 (而一个lambda表达式是在它转换为表达式树或委托类型的上下文只允许。)
这是纯粹的语法糖。你的两个示例代码片段将编译完全相同的IL。它的只是的代表属性的getter或方法的身体的不同的方式。
In a new C# 6.0 we can define methods and properties using lambda expressions.
For instance this property
public string Name { get { return First + " " + Last; } }
can be now defined as follows:
public string Name => First + " " + Last;
The information about expression-boided function members you can find here: http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx
Does anyone know if there's any overhead when using new syntax? Can it slow down (or improve efficiency of) the application or maybe it doesn't matter?
解决方案In a new C# 6.0 we can define methods and properties using lambda expressions.
No, you can't. You can define method and property bodies using syntax which looks like a lambda expression, in that it uses the token
=>
.However, importantly this does not mean that there's a delegate type involved. (Whereas a lambda expression is only permitted in a context where it's converted to an expression tree or delegate type.)
This is purely syntactic sugar. Your two example code snippets will compile to the exact same IL. It's just a different way of representing the body of a property getter or method.
这篇关于在C#6.0的表达浓郁的函数成员的效率和性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!