C # get and set usage

Transfer from https://www.cnblogs.com/zhangtaotqy/p/7998543.html

First find the get and set what is used , in which object-oriented programming, some type of private data is encapsulated, so in order to read and write the corresponding private data, c # use keywords get and set which get responsible for reading the private data , the SET is responsible for writing private data , but the two should not use absolute, I've seen a write operation in get inside.

When we study properties of C # syntax, should first and GET, SET accessor deal, understood in a literal meaning in English, GET should be is to get what, while the SET should be set up something and then we look at, the official is this is how the definition of accessor: get the operation is carried out when reading property, set operation is carried out when setting property. If a property is defined only get, this property is read-only. Similarly, only set, property is write-only, write-only attribute of course I think the significance is not great.

Next we pass some code examples to have contact with GET and SET accessor

class Bank
    {
        private int money;//私有字段

    public int Money  //属性
    {
        //GET访问器,可以理解成另类的方法,返回已经被赋了值的私有变量money
        get { return money;  }
        //SET访问器,将我们打入的值赋给私有变量money
        set { money = value; }
    }
    
}

We can make a vivid example:

Money property like bank automated teller machine, you can not see inside the money, but you can set (to save money), with get (withdrawals). money is a private field, it is packed in class, outside of class programs are not directly accessible, set and get the members of the class is the only way to access external programs like inside the property, as you go to the bank to withdraw money, you can not the money directly from the bank's safe, but banking staff the money taken out for you.
Money that we set the property, how to use it? ?

class Program
    {
        static void Main(string[] args)
         {
            //实例化一个Bank银行
            Bank bank = new Bank();

        //对Money属性做赋值操作,这时我们访问的是SET访问器
        bank.Money = 15;

        //对Money属性做取值操作,这时我们访问的是GET访问器
        int a = bank.Money;
     }
}

Careful friends may find that when we set a breakpoint in the SET's access, code execution bank.Money = 15; when we will enter the property in the SET accessor.

These are the most basic application of our SET and GET's visit a.

But why do we use the GET and SET accessor it? ?
By GET and SET for public variables Money read and write, is in fact an indirect change the value of a private variable of money, that being the case. Why is no money for the public, so that instances of money directly read and write to it? We are not superfluous in it?

In fact, I had very new to GET and SET understand, not to say their meaning difficult to understand, but Why? Why not just use a public field variables in place of it?

Understand one: use the GET and SET allow assignment and increase the limit values

class Bank
    {
        private int money;//私有字段

    public int Money  //属性
    {
        //GET访问器,可以理解成另类的方法,返回已经被赋了值的私有变量money
        get { return money;  }
        //SET访问器,将我们打入的值赋给私有变量money,并且加了限制,不能存负的
          set
        {
            if (value >= 0)
            {
                money = value;
            }
            else
            {
                money = 0; 
            }
        }
    }
    
}

In this case, we would never be able to assign negative attributes Money money up!

In fact, this application only our actual projects, property settings is very common! example:

 public string ApplyStaff
        {
            //延迟加载ApplyStaff对象
            get
            {
                if (this.IsGhost)
                {
                    LazyLoaderFactory.GetLoader().LoadApplicationConfirm(this);
                }
                return applyStaff;
            }
            set
            {
                //判断输入的字符的长度
                if (value.Length > 40)
                {
                    throw new Exception("领用人不能超过40个字符。");
                }
                this.applyStaff = value;
            }
        }

 public string BillName
        {
            get
            {
                return billName;
            }
            set
            {
                if (value==string.Empty)
                {
                    throw new Exception("单据名称不能为空");
                }
                else if (value.Length > 40)
                {
                    throw new Exception("单据名称长度不能超过40个字符");
                }
                else
                {
                    this.billName = value;
                }
            }
        }

If some of the features of this property in the SET accessor, for example, can not be empty, the length of size, must be a number, all kinds of regular expressions, and so on, and then throw an exception, again accept the presentation layer, so that you can write a lot less the verification process.

Guess you like

Origin blog.csdn.net/alexhu2010q/article/details/87895274