Getter without body, Setter with

后端 未结 5 654
死守一世寂寞
死守一世寂寞 2021-01-01 11:21

I have a property which is currently automatic.

public string MyProperty { get; set; }

However, I now need it to perform some action every

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-01 12:07

    You need to either provide a body for both the getter and setter, or neither.

    So if you define either one, it's no longer an auto property.

    So you have to do:

    Either

    public string MyProperty {
        get;
        set;
    }// Automatic property, no implementation
    

    OR

    public string MyProperty
    {
        get { return mMyProperty; }
        set
        {
            mMyProperty = value;
            PerformSomeAction();
        }
    }
    

提交回复
热议问题