Partial method of c#3.0

Source: Internet
Author: User
Tags emit new features


Syntax for C # partial methods



Looking at the what's new in the C # language, it suddenly discovers that the last of the list of features has a "Partial method definitions", but does not have a hyperlink link to its description like other new features. On the Internet, there is very little information about the partial type. In particular, Chinese information, English technical articles, there are two good: http://blogs.msdn.com/wesdyer/archive/2007/05/23/in-case-you-haven-t-heard.aspx and HTTP ://community.bartdesmet.net/blogs/bart/archive/2007/07/28/c-3-0-partial-methods-what-why-and-how.aspx.



I took a closer look. The MSDN Library for Visual Studio 2008 Beta 2, finally has a good understanding of the language features, here to introduce, I hope to help.



The definition of a partial method is similar to a partial type, simply by adding the partial keyword before the method definition. However, a partial method can only be split into two parts-one is the definition declaration and the other is the implementation Declaration (implement declaration). Where the definition declaration looks like an abstract method:



partial class CA
{
// ……
private void partial M (); // definition declaration |
The implementation declaration looks similar to the normal method:
private void partial M () // implementation declaration
{
// method body
}
}



When you call a partial method, it is the same as calling other methods:



CA a = new CA();
a.M();



Only if you define a declaration without writing an implementation declaration, the compiler does not emit (Emit) the metadata and Il code of the method and the statement that called the method. In other words, if the implementation declaration is not written, then the compiled assembly does not have the M method in the CA type.



Considerations for using partial methods



The syntax for a partial method is simple, but there are a few things to note.


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.