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

前端 未结 8 991
清歌不尽
清歌不尽 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:18

    You can do it both the ways.

    If you want to have a class level member variable then do it this way -

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

    Or do this simple in class, if no need for revalidateTransformation() execution call there

    public class sampleClass
    {
    public string mongoFormId {get; set;}
    }
    

提交回复
热议问题