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

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

    Check MSDN Properties Overview

    While a property definition generally includes a private data member, this is not required. The get accessor could return a value without accessing a private data member. One example is a property whose get method returns the system time. Properties enable data hiding, the accessor methods hide the implementation of the property.

    0 讨论(0)
  • 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;}
    }
    
    0 讨论(0)
  • 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();
            }
        }
    
    0 讨论(0)
  • 2021-01-11 18:30

    This won't work since you get a recursive call to the property. If I'm not mistaken, the result will be a StackOverflowException. You must use a variable.

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

    If you don't have to execute revalidateTransformation, you can use the auto-property. This will create a backingfiled for you behind the scene.

    public string MongoFormId { get; set; }
    
    0 讨论(0)
  • 2021-01-11 18:30

    With the code you wrote, you are creating a recursive endless loop on both the get and set. The this keyword refer to the current class, not the property you are in.

    So yes, you need to declare a private field. And to avoid confusion, create properties following the MSDN Naming Guideline (Use Pascal case for properties, camel case for private fields). And please do the same for your methods, it should be RevalidateTransformation instead of revalidateTransformation if you follow the C# convention instead of java's.

    private string mongoFormId;
    public string MongoFormId 
    {
        get 
        { 
            return mongoFormId; 
        }
        set 
        {
            mongoFormId = value;
            RevalidateTransformation();
        }
    }
    
    0 讨论(0)
  • 2021-01-11 18:30
    public string mongoFormId {
        get {
            return this.mongoFormId;
        }
        set {
            this.mongoFormId = value;
            revalidateTransformation();
        }
    }
    

    this way you have the Function recursive on all paths The only way i see is to use a private data member. As other boys tells.

    0 讨论(0)
提交回复
热议问题