Oct 262012
 October 26, 2012  Posted by at 11:04 pm Not So Stupid Questions  Add comments

[To celebrate my first year of programming I will ask a ‘stupid’ questions daily on my blog for a year, to make sure I learn at least 365 new things during my second year as a developer]

partial classes, what can we use them for?

partial classes, what can we use them for?

I have to ask. I don’t remember how this came up,- oh wait I do. At the user group we discussed the several classes in one file, and started talking about partial classes. Today I started thinking a bit more about partial classes. I honestly can’t think of many scenarios when I have used them or would used them. I have when I have worked with autogenerated code, but aside from that I really can’t think up any other good usage for them. When doing some research and reading what Scott Mitchell says about partial classes:

“Partial classes and partial methods are two programming language features of .NET programming languages that make it possible for developers to extend and enhance auto-generated code”

it does seem like I am spot on. But, are there any other good usage for them? Even though they are intended for this particular usage (extending auto-generated code) – can they be nice to have in any other particular scenarios? I tried coming up with ideas, but most of them seem to break the holy rule that classes should not to to many things (in other words no God-classes), and splitting up a class might indicate that it does just that. What do you think?

  8 Responses to “Stupid Question 68: What do we use partial classes for?”

  1. I have only ever seen them used in an effort to cover up for god objects except generated code. In my opinion trying to understand a new codebase or navigate through multiple source files that are the same object is very annoying. So I don’t prefer using them unless I’m using generated code.

  2. It would be useful if you need to share code between several projects. The partial class would contain common methods or structures and each project can embelish that class with the special features they need. So if the common code needs to be enhanced, it can be done in one location.
    It also isolates you from talking with other systems or devices. The particulars of the interaction are in the partial class and if the method of interaction changes, only the patial class needs updating.

  3. I believe the main use is to simplify working with generated code. I think it was added with .net 2 and was used to simplify asp.net.

    In asp,net forms apps when you add a text box or some other control, code gets generated and added to the code behind. This code should never be touched, but prior to partial classes it was imbedded in your code.

    Now it’s also used when working with entity framework. Here it is used to add additional features to generated entities.

    Personally I wouldn’t share a partial class between projects. To start with it must be in the same assembly, and under the same namespace, and class name. Copying and pasting is the only way to achieve this across projects – which is a huge maintenance nightmare. An abstract superclass is probably more appropriate.

    Love the blog btw!

  4. I find partial classes very useful for situations where you have a class that you have generated from a tool such as svcutil.exe, xsd.exe, etc. In these situations having the ability to still add methods, constructors, interfaces, etc to your class without having to worry about losing those modifications if you need to regenerate the class because the Service or Xml Schema that you are serializing from/to has changed.

  5. Partial classes with non-auto-generated code can also make it easy to find and structure certain pieces of code.

    Let’s say that I have a class that implements 4 different interfaces. Each interface is rather complex and requires a few hundred lines of code. With partial classes I can break this out into 4 separate files, one for each interface.

    This prevents me from having to wade through a 1000 line file. It helps me keep private methods in the right place. It’s easy to merge changes as changes to one file should not affect the other.

    Note that this is not required. You can clearly do this is one giant file, you can also use #region to mark out space.

    But as a personal preference, I really like structuring with Partial Classes to minimize the amount of stuff I have to keep in my brain at any given moment.

  6. I found this was required when I was doing Silverlight Development with EF and RIA Services (couple years ago). Every time my data model changed any modifications I made to the auto generated files would be lost. I had to use partial classes to get around this.

  7. At the moment, partial classes are useful because in TypeScript you can use them to create custom properties and methods on built-in DOM objects for easy migration of our existing scripts.

  8. It can be used to support different platforms like WP8 or Win 8.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



What is 5 + 15 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)