C # component development-attributes

Source: Internet
Author: User

Components are some functional classes that can be reused.

Property is used to hide fields in the class. generally, fields are set to private, and restricted access fields are used. Meanwhile, additional logic and rules are encapsulated in the attributes to process all possible values.

Attribute is an intersection of fields and methods.CodeThe access restriction function is generally defined as follows:

Accessmodifier type propertyname

{

Get

{

// Get accessor

}

Set

{

// Set accessor

}

}

Read-only attribute: only the get accessor attribute is included;

Write-only attribute: only the set accessor attribute is included; (generally used for password data protection ).

Although the access syntax for fields and attributes is the same, considering compatibility issues, we should try to use attributes to encapsulate fields.

Automatic attributes:

Public int length {Get; set ;}

The compiler automatically converts the code to the following code:

Private int length;

Public int Length

{

Get

{

Return length;

}

Set

{

Length = value;

}

}

Use attribute to initialize an object -- you can assign values to the attribute of Public set while initializing the object:

Triangle tri = New Triangle ("equilateral triangle") {side1length = 5, side2length = 5, side3length = 5 };

It is a bit similar to the with... Do syntax in VB and Delphi, but only when the object is initialized.

Default property value: In the default class constructor, set the default value for the public set attribute.

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.