Get and set in C #

Source: Internet
Author: User

In the program often encountered get, set, do not understand, in the online query also said the daze, so tidy up, to learn the clear point.

There are two classes of person:

public class Person

{

public string name;

}

public class Person

{

public string Name{set;get;}

}

The Name property of the first type is not encapsulated, its Name property is exposed directly to other classes in the system through the Public keyword, and the Name property of the second type is encapsulated by the Get Set keyword, where get and set correspond respectively to readable and writable, Equivalent to the following code:

private string name;

public string Name

{

get {return name;}

set {name = value;}

}

Just say the difference: When instantiating the first "person" class, the system allocates memory directly to the name attribute when allocating memory space, and then the operation of the Name property is directly manipulating the block where the Name property resides in memory, and when instantiating the second "man" type, The system assigns a private, privately-owned memory space called name (where name is used internally for the class, and name is used for external operations to differentiate). The subsequent read and write operations are associated with name by using a pointer-like object of name, which is used for encapsulation purposes, and can be controlled by the get and set keywords as readable or writable. The latter is recommended, the benefits of encapsulation can refer to the relevant documents, no longer tired of the ~

As for the function of get and set, there are other functions besides controlling read and write, for example, if I want to make some logical judgments when I assign a value to name, I can do this:

private string name;

public string Name

{

get {return name;}

Set

{

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

}

}

To illustrate the following:

If the class is a bank, you can save money and take it.

Private money;

Private Class Bank ()

{

Get

{

return money;

}

Set

{

Money=value;

}

}

Money is like a cash machine in a bank, you can't see it, but you can use Set (save) and get it. Money is a private field that is split in a class, and programs outside the class cannot be accessed directly. In C # in Get, set usage, the set and get members of a class are the only way for an external program to access the internal properties of the class, as if you were going to the bank to get the money, you can't take the money directly from the bank's safe, but the bank's business staff took the money out to you.

A property is like a normal variable to the caller, but as the designer of the class, you can use properties to hide some of the fields in your class so that the outside world can only access your fields through properties, and by using properties to restrict access to your fields, you take advantage of get, set. If you want the user to be free to access your field, then implement get, set, if only want to let the user read the field, only implement get, if only want to let the user write a paragraph only implement set. You can also perform some validation of the values passed by the user in set and get to ensure that your fields will contain the correct values.

Private int A;

public int Index

{

Get

{

return A;

}

Set

{

If (value>0)

A=value;

Else

a=0;

}

}

As you can see, the use of Get and set:

One is to hide a real member within a component or class;

The second is used to establish constraints, such as the realization of "I do not Have you" this constraint;

The third is used to respond to a property change event, when a property change is done, as long as the set method is written in the line.

When you want to read or write the value of a property, the access flag qualifies the implemented statement. The access flag for the value of the Read property is recorded as the keyword get, and the read-write character flag to modify the value of the property is set.

Get and set in C #

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.