Use of get and set methods in C #

Source: Internet
Author: User

I have been confused about this issue. I read this article and feel that the explanation is clear. I hope it will be helpful to others. (Actually reprinted)

 

C # language has two functions: A get function and a set function, which can be clearly seen from the intermediate language code generated by the function. C # It is not recommended to set the protection level of the domain to public so that users can perform any operation outside the class-that is too non-oo, or the specific point is too insecure! For all fields that need to be visible outside the class, C # is recommended to use attributes for representation. The attribute does not represent the storage location, which is the fundamental difference between the attribute and the domain. The following is a typical attribute design:

Using system; <br/> class myclass <br/>{< br/> int integer; <br/> Public int integer <br/>{< br/> get {return integer ;}< br/> set {INTEGER = value ;} <br/>}< br/> class test <br/> {<br/> Public static void main () <br/>{< br/> myclass myobject = new myclass (); <br/> console. write (myobject. integer); <br/> myobject. integer ++; <br/> console. write (myobject. integer); <br/>}< br/>}

As we expect, the program outputs 0 1. We can see that attributes provide a friendly access interface for domain members to programmers through method packaging. Here, value is the key word of C #, which is the implicit parameter of set when we perform attribute operations, that is, the right value when we perform attribute write operations.

Attribute provides read-only (get), write-only (SET), read-write (get and set) Three interface operations. The three operations on the domain must be declared under the same attribute, but they cannot be separated. See the following implementation:

Class myclass <br/>{< br/> private string name; <br/> Public string name <br/>{< br/> get {return name ;} <br/>}< br/> Public string name <br/>{< br/> set {name = value ;}< br/>}< br/>}
The preceding method for separating the name attribute is incorrect! We should put them together like in the previous example. It is worth noting that the three attributes (read-only, write-only, read-write) are considered by C # As the same attribute name. See the following example:

Class myclass <br/>{< br/> protected int num = 0; <br/> Public int num <br/> {<br/> set <br/> {<br/> num = value; <br/>}< br/> class myclassderived: myclass <br/>{< br/> New Public int num <br/>{< br/> Get <br/>{< br/> return num; <br/>}< br/> class test <br/>{< br/> Public static void main () <br/>{< br/> myclassderived myobject = new myclassderived (); <br/> // myobject. num = 1; // error! <Br/> (myclass) myobject). num = 1; <br/>}< br/>}

We can see that the property num-get {} In myclassderived shields the definition of the property num-set {} In myclass.

Of course, the attribute is far more than just the interface operation of the domain. The nature of the attribute is still the method. We can perform some checks, warnings, and other additional operations according to the program logic during Attribute Extraction or assignment, see the following example:

Class myclass <br/>{< br/> private string name; <br/> Public string name <br/>{< br/> get {return name ;} <br/> set <br/>{< br/> If (value = NULL) <br/> name = "Microsoft "; <br/> else <br/> name = value; <br/>}< br/>}

Because of the nature of the attribute method, attributes also have various methods for modification. There are also five types of access modifiers for attributes, but the access modifier for attributes is usually public. Otherwise, we will lose the meaning of attributes as the public interface of the class. In addition to the non-existent feature attributes such as method overloading caused by multiple parameters of methods, virtual, sealed, override, and abstract modifiers perform the same behavior on attributes and methods, however, because attributes are implemented as two methods in essence, we need to pay attention to some of its behaviors. See the following example:

Abstract class A <br/>{< br/> int y; <br/> Public Virtual int x <br/>{< br/> get {return 0 ;} <br/>}< br/> Public Virtual int Y <br/>{< br/> get {return y ;}< br/> set {Y = value ;} <br/>}< br/> public abstract int Z {Get; Set ;}< br/>}< br/> Class B: A <br/>{< br/> int Z; <br/> Public override int x <br/>{< br/> get {return base. X + 1 ;}< br/>}< br/> Public override int Y <br/>{< br/> set {base. y = Value <0? 0: Value ;}< br/>}< br/> Public override int z <br/>{< br/> get {return Z ;} <br/> set {z = value ;}< br/>}< br/>}

This example focuses on some typical behaviors of attributes in the inheritance context. Here, Class A must be declared as abstract because of the existence of abstract attribute Z. In subclass B, the attribute of parent class A is referenced by the base keyword. In Class B, the virtual attribute in Class A can be overwritten only through Y-set.

Static attributes, like static methods, can only access static domain variables of the class. We can also declare external attributes like external methods.

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.