Should we reject such "attributes "??

Source: Internet
Author: User
As we all know, c # has a Property, but what is your understanding of the Property? I would like to express my views on attributes here.
First, the Code is as follows:
Code 1:

Class PropertyExmaple
{
Private string strProp;

Public string Prop
{
Get {return strProp ;}
Set {strProp = value ;}
}

}

In the above Code, a property of Prop is defined, and the get and set accessor attributes are set respectively, so that the property can be read and assigned. In this case, the use of this attribute is no different from that of the public variable. Why should we use the attribute instead of using the public string strProp directly? See the following code:
Code 2:

Class PropertyExample
{
Private string strProp;
Public string Prop
{
Get
{
Return strProp;
}
Set
{
If (value. Length> 8)
{
MessageBox. Show ("beyond the input range! ");
Return;
}
Else
{
StrProp = value;
}
}
}
}

In this way, we verify the validity of the input value in the set attribute. If the value entered by the user exceeds 8 characters, a prompt dialog box is displayed, the variable strProp is not assigned a value, which controls the validity of the value assignment!
Here I want to emphasize that many friends know the usage of attributes, but do not really understand the usefulness of attributes. Check whether your code is similar to the Code in code 1 ............ (If you have any shortcomings in writing an article for the first time, please correct them and criticize them! I am only a c # enthusiast and hope to share with you the joy of programming !)

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.