Home > Software engineering >  Adding a Setter to a => Getter?
Adding a Setter to a => Getter?

Time:09-16

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 => ?

CodePudding user response:

You need a private field for your case. The "property" property cannot set itself.

private bool _property;

public bool property 
{
   get => _property;
   set => _property = value;
}

CodePudding user response:

Too long for a comment but really a comment. There are plenty of examples found with Google, here's one from Microsoft's documentation

class Student
{
    private string _name;  // the name field
    public string Name    // the Name property
    {
        get => _name;
        set => _name = value;
    }
}
  • Related