=> 全局变量中的 Lambda 表达式

=> Lambda expression in global variable

看起来他正试图将全局 "value" 设置为构造函数中的一个字段。

这可能吗?

public class TestLambda
{
    private bool value => inputValue == null;

    public TestLambda(string inputValue)
    {
        // do stuff

    }
}

是的,这是 Expression-bodied 函数成员的有效 C# 6 语法,因为 inputValue 是您的 class.

中的一个字段
public class TestLambda
{
    private string inputValue; // necessary

    private bool value => inputValue == null;

    public TestLambda(string inputValue)
    {
        this.inputValue = inputValue;

    }
}

可以在Roslyns 'New Language Features in C# 6'上找到。

这是 expression-bodied 属性 的 C# 6 语法。这是一个 get-only 属性,返回 inputValue == null

C# 5 及以下等效项是

private bool value
{
    get { return inputValue == null; }
}

是的,这是合法的 C# 6.0 语法,前提是 TestLambda 有一个名为 inputValue 的字段。

没有 expression-bodied 成员的等效代码如下所示:

public class TestLambda
{
    private string inputValue;

    private bool value
    {
        get { return inputValue == null; }
    }

    public TestLambda(string inputValue)
    {
        this.inputValue = inputValue;
    }
}