Public String someproperty
{
// Get accessors do not contain parameters and must return the type of attribute Declaration
Get
{
Return "this is the property value ";
}
// The set accessors do not contain any display parameters, but the compiler assumes that the Set accessors contain a parameter, which is of the same type as the attribute and is expressed as value.
Set
{
// Do whatever needs to be done to set the property
}
}
Privatestring forename;
Publicstring forename
{
Get
{
Return forename;
}
Set
{
If (value. length> 20)
{
// Throw an exception
}
Else
{
Forename = value;
}
}
}
1. Read-only attribute: The set accessors are omitted in the attribute definition; write-only attributes: The get accessors are omitted in the attribute definition (this is a bad programming method)
2. Attribute access modifier:
Pay attention to the following points during use:
- When defining an attribute, the outer layer must have an access level modifier (Public string name)
- When defining the get and set access levels, you can only set one of them separately (the get permission in the following example is the outer permission by default)
- Permissions on the outer layer must be looser than those on the inner layer.
Private string _ name;
Public string name
{
Get
{
Return _ name;
}
Privateset
{
_ Name = value;
}
}
3. automatically implemented attributes: there must be two accessors. You cannot set this attribute to read-only or write-only attributes.
Public String forename {Get; set ;}
Public String forename {Get; privateset ;}