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 !)