Search code examples
c#.netpropertiessetterautomatic-properties

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?


Solution

  • 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.