"C #" Get and set

Source: Internet
Author: User
"Problem" is not a special understanding of get and set, so yesterday I ran into a problem where the code went through a long time. The final result is the physical layer problem, which is actually the problem of got and set. "Explore" 1, get and set are what get accessors, similar to methods, must have a value that returns the property type. So when you use a get accessor, you write this: Get{return ID}
The set accessor, similar to void, has a special variable value when using Set, which is the value when setting the property: Set{id=value}
2, the most direct benefit-security through the Get and set method, the variable privatization, only exposing methods for other classes to access. So to access or change, the change is only your new object, ensuring the security of the data.
3, this is a C # attribute C # when defining a class, you usually encapsulate the objects declared in the class so that the outside world cannot access this property. This allows you to control the external access to private properties. For example, the following code, if you have only get, will only read the value of the property and cannot be assigned a value. If there is only set, then only the set accessor cannot be referenced except as the target of the assignment.
	private string  ID;
            <summary>
            ///primary key, self-
            ///</summary> public
            string ID
            {Get
                {return Id;}
                set {ID =value;}
            }
4, encapsulation, easy to maintain the get and set methods for the encapsulation of the program, for other classes can use the private method of the class. This is a bit of Oo's meaning. Suppose that a variable ID that is used more than 100 times is required to be changed to an uppercase ID, and no encapsulation is made directly to the variable of the class, but is changed 100 times. If you use get and set for encapsulation, just change the ID variable inside the class. 5. Differences between attributes and variables see a metaphor feel special image and accuracy: the ordinary variable is the thing that is put in the room, but the attribute is to put a gatekeeper in the door of the house, you have to carry things to go through the gatekeeper. The gatekeeper is the property accessor, and the thing is get, put things is set.
6, Automatic properties
   Public  class Comment
    {
       //General attribute Usage
                private string  ID;
            public string ID
            {Get
                {return Id;}
                set {ID =value;}
            }

       Auto Property Public
            string Id {get; set;}}}
Automatic properties are actions that do not logically validate a field. When this is written, there is no difference between the two ways of writing. So when to use the general writing, when the automatic writing it? For example: When you need to make some restrictions on a variable.
            public string ID
            {Get
                {return Id;}
                set {
                    if (value). Length <=10)
                    {
                        id =value;
                    }
            }
"Summary" Although this is a small problem, but a lot of problems leak out. Find a lot of things just know what to do, but don't know why. For example, how to use a design mode, their understanding is not very good, internet Baidu, according to realize out, but do not know why this way. such as throwing an exception, only know to Try...catch. Finally.. This write, in the end why to write this, or cooperative acceptance when the Shige sister to explain, before it dawned. Rice teacher said let us first to do, is to let us not have to wait until the Chichi and know the second and then start to realize, before this is timid not advance, but regardless of which way to make, we are to take a good understanding of this part of the knowledge into their own, which I still very deficient, slowly corrected.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.