C # get set functions are very commonly used, but it still requires a lot of experience to use them. The following article will help you accumulate C # get set function experience.
C # get set does not advocate setting 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;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class Default8 : System.Web.UI.Page{ class MyClass { int integer; public int Integer { get { return integer; } set { integer = value; } } } protected void Page_Load(object sender, EventArgs e) { MyClass test = new MyClass(); Label1.Text = test.Integer.ToString(); test.Integer++; Label2.Text = test.Integer.ToString(); }}
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 # get set. It is the implicit parameter of set when we perform attribute operations, that is, the right value when we perform attribute write operations. It is worth noting that three attributes (read-only, write-only, read-write) are considered by C # get set as the same attribute name. See the following example:
Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; public partial class Default8: System. web. UI. page {class MyClass {protected int num = 0; public int Num {set {num = value ;}} class MyClassDerived: myClass {new public int Num {get {return num ;}} protected void Page_Load (object sender, EventArgs e) {MyClassDerived test = new MyClassDerived (); Label1.Text = test. num. toString (); // test. integer ++; // Label2.Text = (MyClass) test ). num. toString (); no get access Failed }}
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:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class Default8 : System.Web.UI.Page{ abstract class A { int y; public virtual int X { get { return 0; } } public virtual int Y { get { return y; } set { y = value; } } public abstract int Z { get; set; } } class B : A { int z; public override int X { get { return base.X + 1; } } public override int Y { set { base.Y = value < 0 ? 0 : value; } } public override int Z { get { return z; } set { z = value; } } } protected void Page_Load(object sender, EventArgs e) { B b = new B(); //b.X = 1; Label1.Text = b.X.ToString(); b.Y = 1; b.Z = 10; Label2.Text = b.Z.ToString(); }}
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. The above is a brief introduction to C # Get set.