[C # Language] C # syntax is a strange place :)

Source: Internet
Author: User
Today, when I was writing code, I found a strange phenomenon: I clearly defined an attribute that can both get and set, however, when I need to call this attribute outside the class, I find that the property cannot be found in intelliisense, but the get_property and set_property methods are found. Strange :)

Let's take a look at the Code:
Public class usercontrolblock: extendedcontrol, ipageblock

{

// The other code

Public String sourcefile

{

Get

{

Return _ sourcefile;

}

Set

{

If (_ sourcefile! = Value)

{

_ Sourcefile = value;

_ Sourceloaded = false;

}

}

}

// The other code

}

The above Code has no syntax errors, and the existing code has been compiled and runs properly. Other attributes can be found normally, but this sourcefile attribute cannot be found, which is really strange! So where does this attribute go ?~~~

The list of members of this class in the Object Browser is also changed to two get/set methods, or the sourcefile attribute cannot be found:


I cannot explain it... By chance, I noticed that this class implements the ipageblock interface. The source code of this interface is as follows:
Public interface ipageblock

{

String sourcefile {Get;} // only getter are declared here.



Bool sourceloaded {Get ;}

Void loadsource ();

Control {Get ;}

}

After reading the declaration about the sourcefile attribute in the interface, I suddenly realized! In the past, the attribute is split into two functions mainly because the interface's declaration of the attribute accessors is inconsistent with the definition of the accessors for the corresponding attributes in the implementation class :)

Find the reason. Here is a brief summary:When the number of accessors corresponding to the attribute in the implementation class is greater than the number of accessors declared in the interface, although there are some inconsistencies, the C # syntax is allowed at this time, it is not considered as an error. The compilation is successful. However, the original intuitive attribute access syntax will be split into two methods starting with get _ and SET _. You should be careful later :)

I do not know whether this problem exists in C #2.0? If you are interested, please try it and let us know. Thank you!

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.