Effects of Get and set properties in C #

Source: Internet
Author: User

C # When defining a class, it is common to encapsulate objects declared in the class so that the property cannot be accessed by the outside world. If you remove the set part of the code above, the outside world can only read the value of name, and if you remove the get part, you can only assign a value to name. This allows you to control the outside access to the private property name, which is a feature of C # .

Of course you can also create a function to value and assign the name, but this is more troublesome.

Attributes are different from normal variables in that they include get and set accessors, which can be used to control access to properties by setting access permissions for the accessors, for example:

private int _old;
public int old{
Get{return _old;}
Set{//added the verification code here
if (value<0)
throw new Argumentoutexception ("value", "input value cannot be less than 0");
_old = value;
}

The above example shows that the properties can be added to the code for processing. You can also set access permissions and so on.

Effects of Get and set properties in C #

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.