Programming terms - field, member, properties (C#)

后端 未结 3 1886
[愿得一人]
[愿得一人] 2021-01-31 17:43

I was trying to find meaning of this terms but especially due to language barrier I was not able to understand what they are used for. I assume that \"field\" is variable (objec

3条回答
  •  春和景丽
    2021-01-31 18:24

    In C# :

    fields : These are variables declared at the class level.

    public class SomeClass
    {
        private int someInteger; // This is a field
        public double someDouble; // This is another field
        protected StringBuidler stringBuidler; // Still another field
    }
    

    properties : Often used as accessors to a private field of a class, they can provide get and set methods that wrap some logic around the field manipulation.

    public class SomeClass
    {
        private StringBuilder stringBuilder;
    
        // Property declaration
        public StringBuilder StringBuilder
        {
            get 
            { 
                if(this.stringBuilder == null)
                    this.stringBuilder = new StringBuidler();
    
                return this.stringBuilder;
            }
            set
            {
                if(this.stringBuilder == null)
                    this.stringbuilder = value;
            }
        }
    }
    

    members : Includes fields, properties, methods, events of a class.

提交回复
热议问题