我看到一些代码说

public int MaxHealth => 
         Memory[Address].IsValid ? 
         Memory[Address].Read<int>(Offs.Life.MaxHp) : 
         0;

现在我对Lambda表达式有点熟悉了。我只是没见过它这样使用。

上述陈述和两者之间的区别是什么

public int MaxHealth  = x ? y:z;

它被称为表达式体成员,是在c# 6中引入的。它只是get only属性上的语法修饰。

它相当于:

public int MaxHealth { get { return Memory[Address].IsValid ?
                             Memory[Address].Read<int>(Offs.Life.MaxHp) : 0; }

一个等效的方法声明是可用的:

public string HelloWorld() => "Hello World";

主要是让你缩短样板。

这是c# 6的一个新特性,称为表达式体成员,它允许您使用类lambda函数定义仅为getter的属性。

虽然它被认为是以下语法糖,但它们可能不会产生相同的IL:

public int MaxHealth
{
    get
    {
        return Memory[Address].IsValid
               ?   Memory[Address].Read<int>(Offs.Life.MaxHp)
               :   0;
    }
}

事实证明,如果您编译上述两个版本并比较为每个版本生成的IL,您将看到它们几乎相同。

下面是在一个名为TestClass的类中定义的经典版本的IL:

.property instance int32 MaxHealth()
{
    .get instance int32 TestClass::get_MaxHealth()
}

.method public hidebysig specialname 
    instance int32 get_MaxHealth () cil managed 
{
    // Method begins at RVA 0x2458
    // Code size 71 (0x47)
    .maxstack 2
    .locals init (
        [0] int32
    )

    IL_0000: nop
    IL_0001: ldarg.0
    IL_0002: ldfld class [mscorlib]System.Collections.Generic.Dictionary`2<int64, class MemoryAddress> TestClass::Memory
    IL_0007: ldarg.0
    IL_0008: ldfld int64 TestClass::Address
    IL_000d: callvirt instance !1 class [mscorlib]System.Collections.Generic.Dictionary`2<int64, class MemoryAddress>::get_Item(!0)
    IL_0012: ldfld bool MemoryAddress::IsValid
    IL_0017: brtrue.s IL_001c

    IL_0019: ldc.i4.0
    IL_001a: br.s IL_0042

    IL_001c: ldarg.0
    IL_001d: ldfld class [mscorlib]System.Collections.Generic.Dictionary`2<int64, class MemoryAddress> TestClass::Memory
    IL_0022: ldarg.0
    IL_0023: ldfld int64 TestClass::Address
    IL_0028: callvirt instance !1 class [mscorlib]System.Collections.Generic.Dictionary`2<int64, class MemoryAddress>::get_Item(!0)
    IL_002d: ldarg.0
    IL_002e: ldfld class Offs TestClass::Offs
    IL_0033: ldfld class Life Offs::Life
    IL_0038: ldfld int64 Life::MaxHp
    IL_003d: callvirt instance !!0 MemoryAddress::Read<int32>(int64)

    IL_0042: stloc.0
    IL_0043: br.s IL_0045

    IL_0045: ldloc.0
    IL_0046: ret
} // end of method TestClass::get_MaxHealth

下面是在一个名为TestClass的类中定义的表达式体成员版本的IL:

.property instance int32 MaxHealth()
{
    .get instance int32 TestClass::get_MaxHealth()
}

.method public hidebysig specialname 
    instance int32 get_MaxHealth () cil managed 
{
    // Method begins at RVA 0x2458
    // Code size 66 (0x42)
    .maxstack 2

    IL_0000: ldarg.0
    IL_0001: ldfld class [mscorlib]System.Collections.Generic.Dictionary`2<int64, class MemoryAddress> TestClass::Memory
    IL_0006: ldarg.0
    IL_0007: ldfld int64 TestClass::Address
    IL_000c: callvirt instance !1 class [mscorlib]System.Collections.Generic.Dictionary`2<int64, class MemoryAddress>::get_Item(!0)
    IL_0011: ldfld bool MemoryAddress::IsValid
    IL_0016: brtrue.s IL_001b

    IL_0018: ldc.i4.0
    IL_0019: br.s IL_0041

    IL_001b: ldarg.0
    IL_001c: ldfld class [mscorlib]System.Collections.Generic.Dictionary`2<int64, class MemoryAddress> TestClass::Memory
    IL_0021: ldarg.0
    IL_0022: ldfld int64 TestClass::Address
    IL_0027: callvirt instance !1 class [mscorlib]System.Collections.Generic.Dictionary`2<int64, class MemoryAddress>::get_Item(!0)
    IL_002c: ldarg.0
    IL_002d: ldfld class Offs TestClass::Offs
    IL_0032: ldfld class Life Offs::Life
    IL_0037: ldfld int64 Life::MaxHp
    IL_003c: callvirt instance !!0 MemoryAddress::Read<int32>(int64)

    IL_0041: ret
} // end of method TestClass::get_MaxHealth

有关c# 6中的这个特性和其他新特性的更多信息,请参阅https://msdn.microsoft.com/en-us/magazine/dn802602.aspx。

请参阅c# 3.0+中属性和字段的区别这篇关于c#中字段和属性getter之间区别的文章。

更新:

注意,在c# 7.0中,表达式体成员被扩展到包括属性、构造函数、终结器和索引器。

您现在看到的是一个表达式体成员,而不是lambda表达式。

当编译器遇到表达式体的属性成员时,它本质上是将其转换为这样的getter:

public int MaxHealth
{
    get
    {
        return Memory[Address].IsValid ? Memory[Address].Read<int>(Offs.Life.MaxHp) : 0;
    }
}

(您可以通过将代码输入TryRoslyn工具来验证这一点。)

表达式体成员——像大多数c# 6特性一样——只是语法糖。这意味着它们不能提供通过现有特性无法实现的功能。相反,这些新特性允许使用更具表现力和更简洁的语法

正如你所看到的,表达式体成员有一些快捷方式,使属性成员更加紧凑:

不需要使用return语句,因为编译器可以推断出您想要返回表达式的结果 不需要创建语句块,因为语句体只是一个表达式 不需要使用get关键字,因为它是由表达式体成员语法的使用暗示的。

我把最后一点加粗了,因为这与你的实际问题有关,我现在就回答你的问题。

……之间的区别。

// expression-bodied member property
public int MaxHealth => x ? y:z;

和…

// field with field initializer
public int MaxHealth = x ? y:z;

和……的区别一样。

public int MaxHealth
{
    get
    {
        return x ? y:z;
    }
}

和…

public int MaxHealth = x ? y:z;

如果你了解属性,这应该是显而易见的。

不过,需要明确的是:第一个清单是一个在底层带有getter的属性,每次访问它时都会调用它。第二个清单是一个带有字段初始化器的字段,当类型实例化时,它的表达式只计算一次。

语法上的这种差异实际上是非常微妙的,可能会导致一个“陷阱”,Bill Wagner在一篇题为“c# 6陷阱:初始化与表达式体成员”的文章中描述了这一点。

虽然表达式体成员类似lambda表达式,但它们不是lambda表达式。基本的区别在于lambda表达式的结果要么是委托实例,要么是表达式树。表达式体成员只是指示编译器在幕后生成属性的指令。相似度(或多或少)以箭头(=>)开始和结束。

我还要补充一点,表达体成员并不局限于属性成员。它们对所有这些成员都起作用:

属性 索引器 方法 运营商

在c# 7.0中添加

构造函数 终结器

但是,它们对以下成员不起作用:

嵌套类型 事件 字段

=>语法等于get {return…}语法。

string MyString { get; } = "value";

不一样吗

string MyString => "value";

尽管在这种情况下它们返回相同的值。观察下面的两个例子。

区别就在这里。 例1将在每次读取属性时返回相同的Person。

例2将在每次读取属性时返回一个新的Person。

//Ex: 1
public Person Person { get; } = new Person();

//Ex: 1
//Is the same as
private readonly Person person = new Person();
public Person Person
{
    get { return person; }
}

和…

//Ex: 2
public Person Person => new Person();

//Ex: 2
//Is the same as
public Person Person
{
    get { return new Person(); }
}

当您使用自动初始化式时,该属性将创建value的实例并持久地使用该值。在上面的帖子中,有一个关于比尔·瓦格纳的坏链接,这解释得很好,我自己搜索了正确的链接来理解它。

在我的情况下,我有我的属性auto初始化一个命令在一个ViewModel的视图。我将属性更改为使用表达式体初始化器,命令CanExecute停止工作。

这是它的样子,这是发生的事情。

Command MyCommand { get; } = new Command();  //works

这是我改的。

Command MyCommand => new Command();  //doesn't work properly

这里的区别是当我使用{get;我在该属性中创建并引用同一个命令。当我使用=>时,我实际上创建了一个新命令,并在每次调用属性时返回它。因此,我永远无法更新我的命令上的CanExecute,因为我总是告诉它更新该命令的新引用。

{ get; } = // same reference
=>         // new reference

尽管如此,如果你只是指向一个支持字段,那么它工作得很好。这只在auto或表达式体创建返回值时发生。

如果你使用的是c# 6,还有一点很重要:

'=>'可以用来代替'get',并且只用于'get only'方法-它不能与'set'一起使用。

对于c# 7,请看下面来自@avenmore的评论——它现在可以在更多的地方使用。这里有一个很好的参考- https://csharp.christiannagel.com/2017/01/25/expressionbodiedmembers/

以下是亚历克斯·布克在他们的回答中分享的声明

当编译器遇到表达式体的属性成员时,它本质上是将其转换为这样的getter:

请看下面的截图,它展示了这个语句(使用SharpLab链接)

public string APIBasePath => Configuration.ToolsAPIBasePath;

皈依

public string APIBasePath
{
    get
    {
        return Configuration.ToolsAPIBasePath;
    }
}

截图:

你也可以使用箭头作为get访问器:

    private string foo = "foo";

    private string bar
    {
        get => $"{foo}bar";
        set
        {
            foo = value;
        }
    }