Partially Speaking…

A good question on Partial Methods

Yesterday in Kansas City I got a question during the LINQ session about partial methods and how they're implemented. This is a new feature found in Orcas Beta 2, and as such, we had a good discussion. I said I'd follow up with a blog post on the topic, so here it is. What are partial methods and why do I care?

Basically a partial method is a way to provide hooks to a class so that it can be extended by users of that class. In the LINQ scenario we are able to add additional processing to events that occur when data is changing in our model. By implementing code against one of the partial methods we can invoke our logic for example when the validation of a data type is called. In our example we threw an exception if the price was set to less than $1.

Partial methods of a given signature can be implemented zero or one times. If the method is not implemented, the compiler optimizes away the call so that no performance hit is taken by the application. If it is, then calls to that method are run through the partial method. Basically the answer to the question of "what if there are multiple partial methods implementing the same signature" is that the compiler will flag an error during the build that the method has already been implemented.

Hope this answers the question. For more detailed information check out the blog posts below: