Adding a Setter to a => Getter?

Viewed 52

I have this read only getter:

public bool property => passedOne && passedTwo;

I want to rewrite it so I can also add a setter to this. I tried following:

public bool property {
    get => passedOne && passedTwo;
    set => property = value;
}

Also tried:

    public bool property {
    get 
    { 
        return passedOne && passedTwo;
    }
    set
    {
        property = value;
    }

But this does not work. How do I add a setter to a getter with => ?

1 Answers

It seems like what you want is to return passedOne && passedTwo unless a value is set. It's not enough to use a bool field for this. You have to know when the value is not set. One way to do this is to use a bool? backing field:

bool? _property;

public bool? Property
{
    get => _property ?? (passedOne && passedTwo);

    set 
    {
        _property=value;
    }
}
Related