I’m still not really getting the upside of partial methods. Can anyone illustrate a problem that partial methods are ideally suited to solve?
I’m still not really getting the upside of partial methods. Can anyone illustrate a
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Essentially, the most useful purpose of partial methods is for a code generation system to provide an API for extending the capability 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, grant the ability to execute validation logic, event notifications, etc. within existing properties.
What makes partial methods appealing is that, if you do not implement them in your own partial class, they are not emitted into the compiled code at all, which provides a modest efficiency boost.
Here’s a decent blog entry that demonstrates the use of partial methods to inject validation logic:
http://www.davidhayden.com/blog/dave/archive/2007/07/24/LINQToSQLValidationEnterpriseLibraryValidationApplicationBlock.aspx