In C #, how do you mix the default 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 set there to set the value? Or do I need to explicitly define getand add another field to the class?

+5
source share
3 answers

You either have a default property, with a support field created by the compiler, as well as the recipient and / or setter element or custom property.

Once you define your own setter, there is no field to create a compiler. You must do this yourself and determine the body of the getter.

+7
source

There is no way.

  • You can have either an automatic or an automatic receiver

    bool Property { get; set; }
    
  • Or implement both manually

    bool Property
    {
        get { return _prop; }
        set { _prop = value; }
    }
    
+9
source

, , , , , :

class Foo
{
    private bool property;
    public bool Property
    {
        get
        {
            return this.property;
        }
        set
        {
            notifySomethingOfTheChange();
            this.property = value
        }
    }
}
+4

All Articles