Wednesday, October 24, 2007

Short Introduction to Partial Methods in .Net 3.5

codeguru - Partial Methods

"Have you heard of partial classes? Partial classes permit defining part of a class in one place and another part somewhere else, usually in another file. Partial classes exist in part so Microsoft can hide the Visual Studio designer plumbing from you. There is nothing sinister here; it's just easier to let the designer manage the declarations and event bindings—in the designer.vb files—and let you write your code in the .vb files. The .designer.vb file contains the partial method declaration—one half—and the .vb file you write code in contains the other half. This model is used for Windows Forms (and probably some other places).

Partial methods are defined in partial classes. Partial methods let you indicate that a private method declared in a partial class may be implemented in the other part of the partial class. Partial methods are stubbed in one place and implemented in another. The most likely scenario where partial methods will be used is in generated code. With generated code, the generator can add partial methods and if you want to insert additional behavior, you can provide an implementation for that method. If you don't provide an implementation for the partial method, nothing happens. Providing the stub is optional.

..."

I'm been trying to wrap my head around Partial Methods that are coming in .Net 3.5. Trying to "get it". At first I didn't and thought it somewhat insane. Partial Classes I got and really like. But having a Method (aka Sub in VB'ness) broken into two parts, in two different files? That sounded like some sort of mad scientist construct. (Nothing against mad scientists of course... ;)

But after reading this article and thinking a little more about it, I think the light has come on (mostly).

I'm now thinking of it like a "template", sort-of kind-of interface or better yet, a code stub.

It's NOT having a method broken into two parts, with an implementation in both. It's a stub in the initial Partial Class (think code that was generated), to be implemented, coded, filled out, fleshed out, etc in the main/working class...

Oh yeah, now I think I get it... Okay, that IS cool.  ;)

No comments: