c # values get, set usage

 Often encountered in the program get, set, I do not quite understand, when online queries can also say stumbled, so in order, in order to learn thoroughly understand the point.

    There are two classes person:

    public class person

    {

      public string name;

    }

    public class person

    {

      public string Name{set;get;}

    }

    The first type of packaging is not name attribute, a name attribute is exposed directly by the public key to other classes in the system, while the second type by the name attribute is encapsulated GET keyword set, get, and set respectively corresponding It is a readable and writable, equivalent to the following code:

    private string name;

    public string Name

    {

      get { return name; }

     set { name = value; }

   }

    It simply the difference: When the first instance of a "person" class, direct the system to allocate memory for the name attribute in the allocation of memory space, after the operation of the name attribute is a direct memory operation where the name attribute of the block; when the second instance of "person" type, the system will be assigned a name called the private private memory space, read and write operations are used to associate name after by Name something similar to this public pointers in order to achieve the purpose of the package, and get and set keywords may also control a write or read. Suggestions can refer to the documentation with the latter benefits, encapsulation, is no longer tired of ~

    As for the role and get set, in addition to other roles outside the control of read and write, give a simple example, say when I want to Name assignment to carry out some logic, you can:

        private string name;

        public string Name

        {

            get { return name; }

            set

            {

             name = String.IsNullOrEmpty(value) ? "空" : value;

            }

        }

 

Further exemplified below:

    Assuming that the class is a bank, can not only save money but also get money

    Private Money;

    Private class bank()

    {

      get

      {

         return Money;

      }

      Set

      {

         Money=value;

      }

    }

    Money is like a bank ATM, you can not see inside of Money, but you can set (to save money), with get (withdrawals). Money is a private field, is dispensed in a class, the class can not program other than directly accessible. C #, get, set usage, 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 take the money directly from the bank's safe, but banking staff the money taken out for you.

    Properties caller seems like an ordinary variable, but as a designer class, you can use the property to hide some of the fields in your class, so that the outside world can only be accessed through the properties of your field, you can attribute limit outside access to your field, you use get, set. If you want users to freely access your field, then you realize get, set; if you want the user to read the field, you just realize get; if only want the user to write field will only achieve set. It also can make some verification of the value that the user passed in the set and get to ensure your field will contain the correct value.

    Private int a;

    Public int index

    {

      get

      {

        return a;

      }

      set

      {

           a = value<0 ? 0 : value ;

      }

    }

    As can be seen, get, set usage is really a hidden internal components or members of the class;

    The second is used to create constraints, for example, and "I did not have you" this constraint;

    The third is used to respond to property change events, when the property changes is to do something, just write in the set method on the line.

    When you want to read or write attribute value, the access flag defining statement is implemented. Access flag value in mind for reading the attribute for the keyword get, and to modify the attribute values ​​read character flag is referred to as a set.

Guess you like

Origin www.cnblogs.com/yuyexiaoxiao/p/12389407.html