Chapter 3, the heartbroken C #, how to use the C # attribute,

Source: Internet
Author: User

Chapter 3, the heartbroken C #, how to use the C # attribute,

Chapter 3. How to Use C # attributes

1. Private access modifierThe access modifier can be used to modify the fields and methods of class members. Each access modifier can only specify specific access permissions for the following members.

If the field or method is declared as public, other classes can access this field or method.

If the field or method is private, this field or method can only be used in this class. If other classes are not accessible, this field or method has the permission to protect the content. However, you can use a third party to modify the content.

Example: use the public method with parameters to pass in the parameter and modify the private field.

Define a dog class name declaration as private and then define a public method as Setname to change the name in the dog class

Input parameters in the Main method

 2. this keyword

In object-oriented programming language, this keyword refers to the current object. Using this keyword can solve the conflict between member variables and local variables.

 

3. attributes in C #

1. Use methods to ensure data security

Example: define a SetAge () method in the dog class to determine the incoming data to avoid logical modification.

 

Code in the main method:

 

2. Use attributes to encapsulate Fields

01. C # attributes

 In C #, fields are usually private. If you want to use fields in the logging class, you must use the get and set accessors. This implementation method combining fields and methods is called attributes.

Get accessors are used to return the values of the corresponding private fields.

The set accesser is used to set the value of the corresponding private field, which is equivalent to the method without returning the value. It uses an implicit input parameter value. When the party assigns a value to the attribute, it will call the set accesser, and assign the new parameter value to the corresponding field

Example: code in the dog class:

The code in the Main method is as follows:

02. Data Type of the attribute

Circled by blue lines is an attribute.

When defining an attribute in a class, the Data Type of the attribute must be consistent with the type of the field it accesses.

Note: The attribute type can be a class or an array.

03. Access type of the attribute

Attributes can restrict data access, and you can set read and write attributes to limit their access types. The access types of attributes can be divided into the following three types:

Read-only attribute: contains only set accessors.

Write-only attribute: contains only get accessors

Read/write attributes: including get and set accessors

04. How to quickly create attributes in Encoding

In a class, set this field to private, select this field ctrl + R + E (the shortcut key for encapsulating the field), and press enter to get the car. You can.

Note: in C #,

001. For the private field of the class, the name must start with an underscore (_), and the subsequent English letters should use the camel name method.

002. Use the Pascal naming method when naming attributes of a class (that is, the first letter of multiple words that make up the attribute name is uppercase)

05. Object Initiator

The object initializer is not used:

Dog dog = new Dog ();

Dog. Age = 6;

Use object initializer:

Dog dog = new Dog () {Age = 6 };

4. Pass values and references

Value transfer:If a value is used for passing a method parameter, the parameter is still the original value after the method is called even if the value of the parameter is modified in the called method.

Value transfer is divided into value type (content is not changed) and reference type (content is changed)

Example (value type ):

 

 

If the output result is customizable, the value will not be changed.

Example (reference type ):

 

Reference transfer:In C #, to transmit parameters by reference, the ref keyword is required to modify the parameters.

Example:

Output result:

Visible reference transfer retains changes to the value

Related Article

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.