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.
say the image point, for example, we define a person class inside a private member of the privately named string name; In the outside we instantiate a man object per, requiring a name for per, and outputting the name, That is to assign a value to Per.name, and then output?? We know from the previous study that the private modified variables in the class are not accessible to the external objects ( cannot directly per.name= "HC666" to fix ^_^) Therefore, only in the class to define a public modified SetName and GetName method 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 indicates write-only ), or both contain (a read-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 name variable
Public string Name //Declare the properties of the variable, read and write
{
get {return name; } //Define a read accessor, which is actually a method
set { name = value; } //define write accessor, the set method has an implied parameter 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 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{//defines 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 Student (); Stu. Name = "HC666"; Execute Write Property Console.WriteLine ("My name is: {0}\t {1} years old this year", Stu. Name,stu. Age); Read Property}}}
The result is the same as the previous example.
Thanks for the support of HC666, welcome your suggestions and comments ^_^ ...
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C # Learning Diary----Properties