Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, how do you mix a default get with an explicit set?

I want to do something like this:

class Foo
{
    bool Property
    {
        get;
        set
        {
            notifySomethingOfTheChange();
            // What should I put here to set the value?
        }
    }
}

Is there anything I can put there to set the value? Or will I have to explicitly define the get and add another field to the class?

like image 584
Matt Avatar asked Jul 10 '12 13:07

Matt


1 Answers

You either have a default property, with compiler-generated backing field and getter and/or setter body, or a custom property.

Once you define your own setter, there is no compiler-generated backing field. You have to make one yourself, and define the getter body also.

like image 115
Ben Voigt Avatar answered Sep 17 '22 18:09

Ben Voigt