0

It looks like he is trying to set the global "value" to a field in the constructor.

Is this even possible?

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

    public TestLambda(string inputValue)
    {
        // do stuff

    }
}
BJ Myers
  • 6,617
  • 6
  • 34
  • 50
  • The `inputValue` used in the initializer for `value` isn't the same one as is used in the TestLambda constructor. – Hutch Dec 29 '15 at 16:44
  • As-is, the code won't compile because `inputValue` doesn't exist in the scope of `value`. – A.Konzel Dec 29 '15 at 16:45

3 Answers3

1

Yes, that is valid C# 6 syntax for a Expression-bodied function member, given that inputValue is a field in your class.

public class TestLambda
{
    private string inputValue; // necessary

    private bool value => inputValue == null;

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

    }
}

It can be found on the Roslyns 'New Language Features in C# 6'.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 1
    For completeness you could post the full property that this is shorthand syntax for. – Lasse V. Karlsen Dec 29 '15 at 16:46
  • That basically was the code. I can't post the actual code for security purposes, but this was on a GUI Form change that he was making just before he left two weeks ago. He was a c# "expert" and was always "updating" our code to the point were it's not stable and we are having to rewrite entire sections of it. The #6 is probably the answer. He probably upgraded to the development environment without telling anyone. – SprintFlunky Dec 29 '15 at 16:56
1

It's a C# 6 syntax for a expression-bodied property. It's a get-only property, returning inputValue == null.

C# 5 and below equivalent is

private bool value
{
    get { return inputValue == null; }
}
Jakub Lortz
  • 14,616
  • 3
  • 25
  • 39
1

Yes, this is legal C# 6.0 syntax, provided that TestLambda has a field called inputValue.

The equivalent code written without the expression-bodied member would look like this:

public class TestLambda
{
    private string inputValue;

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

    public TestLambda(string inputValue)
    {
        this.inputValue = inputValue;
    }
}
BJ Myers
  • 6,617
  • 6
  • 34
  • 50