更新时间:2023-09-06 23:29:16
你在看什么是的前pression健全成员,而不是一个拉姆达前pression。
当编译器遇到一个前pression浓郁的属性成员,这将从根本上把它转换成一个 GET
之三,是这样
公众诠释MaxHealth
{
得到
{
返回内存[联系地址] .IsValid?内存[地址] .Read< INT>(Offs.Life.MaxHp):0;
}
}
(你可以抽code到一个名为工具 TryRoslyn 。)
防爆pression健全成员 - 最喜欢的C#6的特性 - 是的只是的的语法糖。这意味着它们不提供否则不能通过现有的功能来实现的功能。相反,这些新功能,允许使用更前pressive和简洁的语法
正如你所看到的,前pression健全成员有快捷键了一把,让物业人员更加紧凑:
收益
语句,因为编译器可以推断出你想要回前pression GET
关键字,因为它是由使用前pression健全成员语法的暗示。 我所做的最后一点大胆的,因为这是有关您的实际问题,我现在回答。
...之间的差异
//前pression浓郁的成员属性
公众诠释MaxHealth => X ? Y:Z者除外;
和...
//场场初始化
公众诠释MaxHealth = X? Y:Z者除外;
时一样...
之间的差 公众诠释MaxHealth
{
得到
{
返回X? Y:Z者除外;
}
}
和...
公众诠释MaxHealth = X? Y:Z者除外;
其中 - 如果你理解性能 - 应该是显而易见的。
只是要清楚,虽然:第一个列表与将要每次访问时称引擎盖下获取方法的属性。第二个列表是一个字段初始值,其前pression只计算一次,当类型实例化的字段。
在语法这种差别实际上相当细微,并可能导致一个疑难杂症,这是由比尔瓦格纳在题为交 A C#6疑难杂症:初始化与前pression酒体成员。的
虽然前pression健全成员是拉姆达前pression-的如的,他们的不的拉姆达前pressions。最根本的区别是,一个lambda前pression结果在任何一个委托实例或前pression树。防爆pression健全成员只是一个指令编译器生成幕后的属性。相似性(或多或少)开始,并以箭头结束( =>
)。
我还要补充一点,前pression健全成员不限于财产的成员。他们对所有这些成员的工作:
不过,他们并不对这些成员的工作:
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:
return
statement because the compiler can infer that you want to return the result of the expressionget
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:
However, they do not work on these members: