Get and set in C,

Source: Internet
Author: User

Get and set in C,

Http://blog.sina.com.cn/s/blog_82526aa60100txtx.html

Get and set are often encountered in the program, but they are not quite clear. They are also confused during online queries, so they can be well understood.

There are two classes of person:

Public class person

{

Public string name;

}

Public class person

{

Public string Name {set; get ;}

}

The name attribute of the first type is not encapsulated, and is directly exposed to other classes in the system through the public keyword. The name attribute of the second type is encapsulated by the get set keyword, get and set correspond to readable and writeable respectively, which is equivalent to the following code:

Private string name;

Public string Name

{

Get {return name ;}

Set {name = value ;}

}

Simply put, the difference is: when the first "person" class is instantiated, the system directly allocates memory for the name attribute when allocating memory space, then, the operation on the name attribute directly operates the block where the name attribute in the memory is located. When the second "person" type is instantiated, the system allocates a private memory space named name (the name here is used internally for the class and the Name is used for external operations, which must be differentiated ), subsequent read and write operations are performed through the Name public something similar to a pointer to associate the name, in order to achieve the purpose of encapsulation, And the get and set keywords can also be used to control whether it is readable or writable. We recommend that you use the latter. For more information about the advantages of encapsulation, see relevant documents ~

As for the functions of get and set, in addition to controlling read/write, there are other functions. For example, when I assign a value to a Name, I want to perform some logical judgment first, you can do this:

Private string name;

Public string Name

{

Get {return name ;}

Set

{

Name = String. IsNullOrEmpty (value )? "Null": value;

}

}

 

The following is an example:

Assume that a bank can save money and withdraw money.

Private Money;

Private class bank ()

{

Get

{

Return Money;

}

Set

{

Money = value;

}

}

Money is like an ATM in a bank. You can't see the Money in it, but you can use set to save Money and get to get Money ). Money is a private field and is encapsulated in a class. Programs other than the class cannot directly access it. In C # get and set usage, the set and get members of the class are the only method for internal attributes of the external program's category class, just as you go to the bank to get money, you cannot take money directly from the bank's safe, but the bank's business staff give you the money.

Attributes look like a common variable to the caller. But as a class designer, you can use attributes to hide some fields in your class so that the outside world can only access your fields through attributes, you can use properties to restrict external access to your fields and use get and set. If you want to allow users to access your fields at will, you can implement get and set. If you only want users to read fields, you can only implement get. If you only want users to write segments, you can only implement set. At the same time, you can perform some verification on user-passed values in set and get to ensure that your field will contain correct values.

Private int;

Public int index

{

Get

{

Return;

}

Set

{

If (value> 0)

A = value;

Else

A = 0;

}

}

It can be seen that the get and set operations are real members inside the hidden component or class;

Second, it is used to establish constraints, for example, implementing the constraint "I have no you;

Third, it is used to respond to attribute change events. When attribute changes are done, you only need to write them in the set method.

When you want to read or write the attribute value, the access flag limits the implemented statement. The access Mark used to read the attribute value is recorded as the keyword get, and the read/write mark used to modify the attribute value is recorded as set.

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.