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.