c# property setter body without declaring a class-level property variable

前端 未结 8 992
清歌不尽
清歌不尽 2021-01-11 17:38

Do I need to declare a class-level variable to hold a property, or can I just refer to self.{propertyname} in the getter/setter?

In other words, can I d

8条回答
  •  感动是毒
    2021-01-11 18:29

    You need to set a field variable and store the value there, if you're going to use custom getter and setter.

    With the code you have right now you will be running into a stack overflow exception. When you assign something to mongoFormId, you'll execute the line this.MongoFormId = value;. This is an assignment to mongoFormId, resulting in executing the line this.MongoFormId = value;, and so on. It won't ever stop.

    The correct way is a field:

        private string _mongoFormId;
        public string mongoFormId {
            get { return this._mongoFormId; }
            set {
                this._mongoFormId = value;
                revalidateTransformation();
            }
        }
    

提交回复
热议问题