Partial classes – they have only one real use, right?

Over the years I have heard that some folk actually use partial classes to help assist in-team collaboration efforts, yet I have yet to meet such people. If you have a standard source control system that allows you to merge then surely this is just as good?

The reason I bring this up is that I was recently extending some code that is designer driven, well that’s not really true there is no designer but what the program generates is some generic code that is partial so you can cleanly extend it without wading through all the nonsensical automated comments and clear lack of sane code formatting.

If you were to look on MSDN there are two major points which are mentioned about the partial keyword (shamelessly lifted from the MSDN site):

  • When working on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously.
  • When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when creating Windows Forms, Web Service wrapper code, and so on. You can create code that uses these classes without having to edit the file created by Visual Studio.

Now I can totally see that partial classes for designer driven code is very good, e.g. WinForms which if you use the designer to build just throws in a tonne of glue for you that you really don’t want to damage your eyes by looking at – but do we really use partial classes in these large projects?

I’d be interested to know if folk have actually used partial classes in the way in which the first point alludes to from MSDN.

p.s. (final reminder) please post any comments on my new blog –

10 thoughts on “Partial classes – they have only one real use, right?

  1. One of the larger places I used partial classes is for extending proxy’s generated via wsdl.exe for Web Service’s. Much of the time you need to extend the set of properties/methods on these proxy’s.

    A simple example, is that the old way which wsdl.exe worked is that it would only emit fields, not properties. And you can only databind to properties. So in this case we used to have to extend the partial class with the list of fields needed for binding…

  2. Recently I was working with an open source framework. There was a class that didn’t have a method I wanted, so I added it using partial classes. This might be what MSDN had in mind, working with code that you don’t own.

  3. @Scott – good point, I had not thought of that. Would if have not been better to derive from that class though you wanted to extend? assuming it was not sealed of course.

    I’m still pretty unconvinced that people do use partial classes inline with that 1st point on MSDN. Seems to be pretty much designer exclusive.

  4. I have found partial classes very useful of late. While refactoring some classes I find it a lot easier to put deprecated code in a separate physical file, which allows me to navigate and focus on the good stuff a lot more easily.

  5. @Guy – that actually sounds like a pretty clean solution to that sort of thing, I may have to *borrow* that idea sometime in the future.

  6. I actually found a nice use for partial classes.

    On some of my larger objects, I use partial classes to separate static methods from instance methods.

    I like doing that a lot better than using Regions, which I really can’t stand.

    I haven’t tried it, but I imagine that for very large classes, separate files could be created for properties, constructor, methods, statics, etc.

    Bottom line is that I love having the option. Partial classes were a nice idea.

  7. It’s because M$ Visual Source Safe doesn’t have good support for merging patches. It’s designed for checkout to block all other attempts to checkout.

Leave a Reply

Your email address will not be published. Required fields are marked *