C # key Knowledge (iii) (turn)

Source: Internet
Author: User
Detailed Chapter Three: Class attribute
The use of RAD development tools of a certain inspector is very familiar with the programmer through it can manipulate the properties of the object, Delphi introduced the Publish keyword to advertise object properties by programmers generally welcome. Access to private members through access flags, in C # There are two ways to expose a named property of a class-either through a domain member or through a property. The former is implemented as a member variable with public access, and the latter does not respond directly to the storage location, but is accessed through the access flag (accessors). When you want to read or write the value of a property, the access flag qualifies the implemented statement. The access flag used to read the value of the property is the keyword get, and the read-write flag of the value to modify the property is set.

Class properties

can only read get
can only write set
Readable and writable Set/get


Take a look at the example:

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 a class's internal storage structure, you should use an Access flag. The access flag passes a new value to the property in the value parameter. At the same time, you can get the chance to implement adding valid code to the SET flag.

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.