Learn more about the differences between implicit and implemented value attributes (get {}, set {} accessors In the attributes)

Source: Internet
Author: User

 

// Define public string name {Get; Set ;}// implicit private string name; Public string name {get {return name;} in the property ;} set {name = value ;}}

Both are used to encapsulate fields for data security.

Normally, the two are used in the same way. The previous method is used by the compiler to generate the latter code;

Public string name {Get; set;} can be written after dotnet2.0 to reduce the workload of programmers.

Of course, if simple get is not implemented by writing, set cannot be implemented by writing. However, if the abstract keyword abstract is added before, the corresponding non-write implementation will be the same as the abstract method;
Get and set can also be added with public and other access levels.

 

The difference is:

If you want to make some judgment on the attribute, such as the string cannot be empty, the integer must be greater than zero, and so on, you cannot use it.

 

public string name{get;set;}

 

Such convenience must be honestly written

 

public string Name{    get { return name; }    set { name= value; }}

Such code

 

 

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.