C # Explanation of key knowledge (III)

Source: Internet
Author: User

Chapter 3: class attributes
Certain inspector who have used RAD development tools are familiar with it. programmers can use it to operate on Object Attributes. The PUBLISH keyword is introduced in DELPHI to PUBLISH object attributes, which is widely welcomed by programmers. access a private member through an access flag. in c #, there are two ways to reveal the class's naming attributes-either through a domain member or through an attribute. The former is implemented as a member variable with public access; the latter does not directly respond to the storage location, but is only accessed through the accessors. When you want to read or write the attribute value, the access flag limits the implemented statement. The access Mark used to read the attribute value is recorded as the keyword get, and the read/write mark used to modify the attribute value is recorded as set.

Class attributes

Read Only get
Only set can be written.
Readable and writable set/get


See the example below:

Using System;

Public class Test
{
Private int m_nWrite;
Private int readonly m_nRead = 100;
Private int m_nWriteRead;

Public int WRITEREAD
{
Get {return m_nWriteRead ;}
Set {m_nWriteRead = value ;}
}

Public int WRITE
{
Set {m_nWrite = value ;}
}

Public int READ
{
Get {return m_nRead ;}
}

}


Class TestApp
{
Public static void Main ()
{
Test MyTest = new Test ();
Int I = MyTest. READ; // get
MyTest. WRITE = 250; // set
MyTest. WRITEREAD + = 10000000; // set and get

Console. WriteLine ("get: {0} set: {1} set/get: {2}", I, MyTest. WRITE, MyTest. WRITEREAD );
}
}

If you want to hide the details of the class's internal storage structure, you should use an access flag. The access flag transmits a new value to the attribute in the value parameter. At the same time, you can get the opportunity to add valid code in the set flag.


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.