Please illustrate the problem that partial methods solve.

I'm still not very good at partial methods. Can someone illustrate the problem that partial methods are perfect for solving?

+4
source share
4 answers

In fact, the most useful goal of partial methods is for the code generation system to provide an API to extend the capabilities of its properties and methods without using inheritance.

Take a look at any Linq to SQL data model for a quick example.

The generated code includes partial methods, which, if implemented in your own partial class, provide the ability to execute validation logic, event notifications, etc. within existing properties.

What makes partial methods attractive is that if you don't implement them in your own partial class, they don’t get into compiled code at all, which provides a moderate increase in efficiency.

Here's a decent blog post that demonstrates the use of partial methods to introduce validation logic:

http://www.davidhayden.com/blog/dave/archive/2007/07/24/LINQToSQLValidationEnterpriseLibraryValidationApplicationBlock.aspx

+6
source

From Partial Class and Methods (C # Programming Guide) to MSDN:

A partial class or structure may contain a partial method. One part of the class contains a method signature. An optional implementation may be defined in the same part or another part. If no implementation is specified, then the method and all method calls are deleted at compile time.

Partial methods allow the developer of one part of the class to define a method that is similar to an event. The developer of another part of the class can decide whether to implement the method or not. If this method is not implemented, the compiler removes the method signature and all method calls. Method calls, including any results that may occur when evaluating arguments in calls, do not affect runtime. Therefore, any code in a partial class is free to use the partial method, even if an implementation is not provided. Compilation errors or runtime errors will not occur if the method is called but not implemented.

Partial methods are especially useful for customizing the generated code. They allow you to reserve the method name and signature so that the generated code can call the method, but the developer can decide whether to implement the method. Like partial classes, partial methods allow you to create code created by a code generator and code created by a human developer to work together without costing runtime.

In my opinion, I would recommend not using them unless you have a specific, specific need for them.

+10
source

The generated code. Simple and simple, that was the number one reason they were implemented. Look at something like WPF. The user interface runs declaratively in XAML, and "code-behind" in C #. Both parts are the same class partition using the concept of a partial class

+3
source

As I understand it, one of the main advantages is the ability to create a "stub" generated by the code, which you can choose whether to implement. So your gen code creates a partial method and calls some Validate method. To β€œhook up” your test, you simply implement a partial method. The partial keyword allows a relatively clean development process.

+2
source

All Articles