C #: Attributes

Source: Internet
Author: User

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 ;}

 

 

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.