A property can be a named member of a Class (class), struct (struct), interface (interface), and he provides a flexible mechanism to read, write, or compute the value of a private field, which can be considered a member of a public field. But he actually defines an "accessor" in a special way so that the value of a private field can be read or written or manipulated.
To say the image point, for example, we define a person class inside a private member of the secret string name; In the outside we instantiate a person object per, requiring a name of per, and outputting the name, that is, assigning a value to Per.name, and then outputting?? As we know from the previous study, the private modified variables in the class are not accessible to the external objects, (cannot directly per.name= "HC666" to fix the ^_^), so you can only define a public decorated SetName and GetName method in the class will " HC666 "as a parameter to write and output, this is a bit cumbersome, this time the" property "should be on the stage, we define a read-write property name for name can compensate for this deficiency.
"Accessor"
The accessor of a property contains an executable statement that Gets or sets the property, and the declaration of the accessor can contain a get accessor (which means read-only), or a set accessor (which means write-only), or both (which indicates a readable writable). (In the example above we use the get accessor to output name, write using the set accessor)
To define a property:
In the example above, we can define a read-write property name for name, using the public modifier that the external object can access
private string name; Declare the name variable public string name //Declare the property of the variable, read and write { get {return Name; } The definition of a read accessor is actually a method set {name = value; } Defines a write accessor, the set method has an implied parameter of value }
Define a read-only property, such as our age is fixed read-only
private UINT age=10; Declare variable age private string Age //Declare a variable's property, read only { get {return age; } Read Accessor }
An instance of the property:
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Namespace Test1 { class person { //define variable name, and define a read-write property for name private string name; Define property Public string name {//accessor get {return Name;} set {name = value;}//set comes with a value parameter } //Defines the variable age and defines a read-only property for age private uint age=10; Public uint Age { get {return-age;}} } Class program { static void Main (string[] args) {person per = new Person (); Per. Name = "HC666"; Execute Write Property Console.WriteLine ("My name is: {0}\t {1} year old", per.) Name,per. Age); Read Property } } }
Results:
Abstract Properties:
As mentioned earlier, attributes can make members of classes, structs, interfaces, or abstract properties of abstract classes, and abstract properties are implemented as abstract methods in derived classes.
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; namespace Test1 {//define a person abstract class abstract classes person {//define abstract Properties public abstract string Name {//read/write get; Set } public abstract UINT-age {//read-only get; }}//define derived class class Student:person {private string name; private UINT age=10; Implement abstract property public override string name {get {return Name; } set {Name=value; }} public override uint-age {get {return-age;} }} class Program {static void Main (string[] args) {Student Stu = new stud ENT (); Stu. Name = "HC666"; Execute Write Property Console.WriteLine ("My name is: {0}\t this year {1} years old, "Stu. Name,stu. Age); Read Property}}}
The result is the same as the previous example.
The above is the C # learning diary----Attributes of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!