Shawn Weisfeld

I find when I talk to myself nobody listens. - Shawn Weisfeld
posts - 356, comments - 173, trackbacks - 34

My Links

News

The views expressed in this blog are mine and mine alone, not that of my employer, Microsoft, or anyone else’s. No warrantee is given for the quality of any material on this site.

Archives

Post Categories

Partial Classes VB vs C#

Stumbled across this during a discussion of RIA services today. . . .

Apparently in VB you can have the “partial” modifier on only 1/2 of a partial class. However in C# you are required to put the partial modifier on both halves of the class. I will let you guys argue about what way is better. . . . I think that both have merits. . .

image

Print | posted on Monday, July 04, 2011 2:41 PM |

Feedback

Gravatar

# Partial classes are for generated code

The reason that partial classes were invented, and the primary motivation for using them, is to separate hand-written code from generated code.

If you believe that premise, then we can use it to evaluate our options. In C#, the code generator has to emit the "partial" keyword. And then the coder has to use the "partial" keyword. The code generator knows that the class was intended to be extended. The coder knows that he is extending a class.

In VB, which side does the "Partial" keyword go on, the generated code or the extension? If it goes on the generated code, then the coder doesn't "know" that he's extending a class (he does, of course, but it isn't explicit). If it goes on the hand-coded half, then the coder is again being explicit.

Given this analysis, I prefer the VB style, with generated code unadorned. This lets a coder explicitly extend a class, but does not require them to do so. In C#, the code generator would simply emit the "partial" keyword, even though only one "half" exists.

BTW, is it possible to have more than one "Partial" in VB?
7/5/2011 11:37 AM | Michael L Perry

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 5 and 6 and type the answer here:

Powered by: