C # Get and set accessors

Source: Internet
Author: User

The accessors of an attribute include executable statements related to the obtained (read or calculated) or set (write) attributes. The accessors declaration can contain get accessors or set accessors, or both. The statement takes one of the following forms:

Get {}

Set {}

Get accessors
The get accessor body is similar to the method body. It must return the value of the attribute type. Executing the get accessors is equivalent to reading the field value. The following is the get accesser that returns the value of the private field name:

Private string name; // The Name field
Public string name // The Name property
{
Get
{
Return name;
}
}
When a property is referenced, The get accessor is called to read the value of the property unless the property is assigned to the target. For example:

Employee e1 = new employee ();
...
Console. Write (e1.name); // The get accessor is invoked here
The get accessors must be terminated in the return or throw statement and cannot exceed the accessors.

Set accessors
The Set accessor method is similar to the void method returned. It uses an implicit parameter called value. The type of this parameter is the type of the attribute. In the following example, the set accessors are added to the name attribute:

Public string name
{
Get
{
Return name;
}
Set
{
Name = value;
}
}
When assigning values to attributes, call the set accessors with parameters that provide new values. For example:

E1.name = "Joe"; // The set accessor is invoked here
It is wrong to use the implicit parameter name (value) for the local variable declaration in the Set accessors.

Remarks
Attributes are classified according to the accessors used as follows:

The attribute with only the get accessors is called the read-only attribute. The read-only attribute cannot be assigned a value.
The attribute with only the set accessors is called the write-only attribute. You cannot reference a property other than the target of the value assignment.
The get and set accessors are read/write attributes.
In the attribute Declaration, both get and set accessors must be declared inside the attribute body.

Using the get accessors to change the object status is a wrong programming style. For example, the following accessors may cause side effects of changing the object status every time they access the number field.

Public int number
{
Get
{
Return number ++; // don't do this
}
}
You can use the get accessor to return a field value or calculate a field value. For example:

Public string name
{
Get
{
Return name! = NULL? Name: "Na ";
}
}
In the above Code segment, if the name attribute is not assigned, it returns Na.

Example 1
This example shows how to access the hidden attributes of another attribute with the same name in the base class.
// Property_hiding.cs
// Property hiding
Using system;
Public class baseclass
{
Private string name;
Public string name
{
Get
{
Return name;
}
Set
{
Name = value;
}
}
}

Public class derivedclass: baseclass
{
Private string name;
Public new string name // notice the use of the new Modifier
{
Get
{
Return name;
}
Set
{
Name = value;
}
}
}

Public class mainclass
{
Public static void main ()
{
Derivedclass d1 = new derivedclass ();
D1.name = "John"; // derived class Property
Console. writeline ("name in the derived class is: {0}", d1.name );
(Baseclass) D1). Name = "Mary"; // base class Property
Console. writeline ("name in the base class is: {0 }",
(Baseclass) D1). Name );
}
}
Output
Name in the derived class is: John
Name in the base class is: Mary
The focus shown in the preceding example is as follows:
The property name in the derived class hides the property name in the base class. In this case, the attribute Declaration of the derived class uses the new modifier:
Public new string name
{
...
The conversion (baseclass) is used to access the hidden attributes in the base class:
(Baseclass) D1). Name = "Mary ";
 

 

 

++

Get set is used to extend the domain, that is, the variables in C, but it is more flexible to use. See the following code:
Using system;
Public class CLS
{
Private int book; // defines a domain (also called a variable, but it is more convenient to use it for calls like this)
Public int book
{
Get
{
Console. writeline ("DSF ");
Return book;

}
Set
{
Console. writeline ("AAA ");
Book = value;

}
}
Public static void main ()
{
Cls obj = new Cls ();
OBJ. Book = 120; // note this sentence
Console. writeline (obj. Book );
}

}
The running result is:
Aaa
DSF
120
That is, first assign 120 to the value (equivalent to a bucket), and then return it to the book with get {}, which is actually a process of changing the variable value.
Why is it so complicated? Because nested statements are used, it is easier to write other statements. You should learn it later.
If not:
OBJ. Book = 120;
What is the result?
It is only a process of value: Get {} but not set {}
Result:
DSF
0

 

 

From: http://hi.baidu.com/pxy59/blog/item/ddb40f731e337d1c8601b026.html

Http://zhidao.baidu.com/question/4889679.html

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.