什么是=>分配在C#中的属性签名 [英] What is the => assignment in C# in a property signature
问题描述
我碰到一些code,它说出来。
公众诠释MaxHealth =>内存[联系地址] .IsValid?内存[地址] .Read< INT>(Offs.Life.MaxHp):0;
现在我有些熟悉的lambda前pressions。我只是还没有看到它使用这种方式。
什么是上面的语句之间的差异以及</ p>
公众诠释MaxHealth = X? Y:Z者除外;
你在看什么是的前pression健全成员,而不是一个拉姆达前pression。
当编译器遇到一个前pression浓郁的属性成员,这将从根本上把它转换成一个 GET
之三,是这样
公众诠释MaxHealth
{
得到
{
返回内存[联系地址] .IsValid?内存[地址] .Read&LT; INT&GT;(Offs.Life.MaxHp):0;
}
}
(你可以抽code到一个名为工具<一个验证这一点为自己href=\"http://tryroslyn.azurewebsites.net/#K4Zwlgdg5gBAygTxAFwKYFsDcAoADsAIwBswBjGUogQxBBgGEYBvbGNmfYsmANwHswAExgBZABQBKZq3YBfbLKAA\"相对=nofollow> TryRoslyn 。)
防爆pression健全成员 - 最喜欢的C#6的特性 - 是的只是的的语法糖。这意味着它们不提供否则不能通过现有的功能来实现的功能。相反,这些新功能,允许使用更前pressive和简洁的语法
正如你所看到的,前pression健全成员有快捷键了一把,让物业人员更加紧凑:
- 有没有必要使用
收益
语句,因为编译器可以推断出你想要回前pression 的结果 - 有没有必要建立一个语句块,因为身体只有一个前pression
- 有没有必要使用
GET
关键字,因为它是由使用前pression健全成员语法的暗示。
我所做的最后一点大胆的,因为这是有关您的实际问题,我现在回答。
...之间的差异
//前pression浓郁的成员属性
公众诠释MaxHealth =&GT; X ? Y:Z者除外;
和...
//场场初始化
公众诠释MaxHealth = X? Y:Z者除外;
时一样...
之间的差 公众诠释MaxHealth
{
得到
{
返回X? Y:Z者除外;
}
}
和...
公众诠释MaxHealth = X? Y:Z者除外;
其中 - 如果你理解性能 - 应该是显而易见的。
只是要清楚,虽然:第一个列表与将要每次访问时称引擎盖下获取方法的属性。第二个列表是一个字段初始值,其前pression只计算一次,当类型实例化的字段。
在语法这种差别实际上相当细微,并可能导致一个疑难杂症,这是由比尔瓦格纳在题为交<记载的href=\"http://thebillwagner.com/Blog/Item/2015-07-16-AC6gotchaInitializationvsEx$p$pssionBodiedMembers\"相对=nofollow> A C#6疑难杂症:初始化与前pression酒体成员。的
虽然前pression健全成员是拉姆达前pression-的如的,他们的不的拉姆达前pressions。最根本的区别是,一个lambda前pression结果在任何一个委托实例或前pression树。防爆pression健全成员只是一个指令编译器生成幕后的属性。相似性(或多或少)开始,并以箭头结束( =&GT;
)。
我还要补充一点,前pression健全成员不限于财产的成员。他们对所有这些成员的工作:
- 属性
- 索引器
- 方法
- 运营商
不过,他们并不对这些成员的工作:
- 构造
- Deconstructors
- 嵌套类型
- 活动
- 字段
I came across some code that said
public int MaxHealth => Memory[Address].IsValid ? Memory[Address].Read<int>(Offs.Life.MaxHp) : 0;
Now I am somewhat familiar with Lambda expressions. I just have not seen it used it this way.
What would be the difference between the above statement and
public int MaxHealth = x ? y:z;
What you're looking at is an expression-bodied member, not a lambda expression.
When the compiler encounters an expression-bodied property member, it will essentially convert it into a get
ter, like this:
public int MaxHealth
{
get
{
return Memory[Address].IsValid ? Memory[Address].Read<int>(Offs.Life.MaxHp) : 0;
}
}
(You can verify this for yourself by pumping the code into a tool called TryRoslyn.)
Expression-bodied members - like most C# 6 features - are just syntactic sugar. This means that they don’t provide functionality that couldn't otherwise be achieved through existing features. Instead, these new features allow a more expressive and succinct syntax to be used
As you can see, expression-bodied members have a handful of shortcuts that make property members more compact:
- There is no need to use a
return
statement because the compiler can infer that you want to return the result of the expression - There is no need to create a statement block because the body is only one expression
- There is no need to use the
get
keyword because it is implied by the use of the expression-bodied member syntax.
I have made the final point bold because it is relevant to your actual question, which I will answer now.
The difference between...
// expression-bodied member property
public int MaxHealth => x ? y:z;
And...
// field with field initializer
public int MaxHealth = x ? y:z;
Is the same as the difference between...
public int MaxHealth
{
get
{
return x ? y:z;
}
}
And...
public int MaxHealth = x ? y:z;
Which - if you understand properties - should be obvious.
Just to be clear, though: the first listing is a property with a getter under the hood that will be called each time you access it. The second listing is is a field with a field initializer, whose expression is only evaluated once, when the type is instantiated.
This difference in syntax is actually quite subtle and can lead to a "gotcha" which is described by Bill Wagner in a post entitled "A C# 6 gotcha: Initialization vs. Expression Bodied Members".
While expression-bodied members are lambda expression-like, they are not lambda expressions. The fundamental difference is that a lambda expression results in either a delegate instance or an expression tree. Expression-bodied members are just a directive to the compiler to generate a property behind the scenes. The similarity (more or less) starts and end with the arrow (=>
).
I'll also add that expression-bodied members are not limited to property members. They work on all these members:
- Properties
- Indexers
- Methods
- Operators
However, they do not work on these members:
- Constructors
- Deconstructors
- Nested Types
- Events
- Fields
这篇关于什么是=&GT;分配在C#中的属性签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!