There are plenty of cases where a partial class can assist
you in developing your applications. Most people have already seen them and
might not have noticed them in ASP.NET pages, because you don't actually need
to know that it is using these partial classes in order to use them. They're
also quite popular being used alongside generated code, because it lets people
modify the generated code. Sometimes people will also use it when working in
larger groups who will not be committing code often. This is because it lets
them make large amounts of changes without having painful merges later.
ASP.NET Pages Already Use Partial Classes
First I will remind you again of the ASP.NET pages. Every
page you create in ASP.NET is eventually compiled. Every .aspx file will be
used to declare a page class. They all inherit from a standard Page class. You
can see this in your code behind file. If you view the code of your ASP.NET
page you will see a class definition. It will have the partial keyword in the
definition and it will look similar to this code.
Listing 3: Codebehind File
public partial class MyWebPage : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
// Perform some action here.
Since there will be a class created from your ASP.NET code
in the .aspx file this must be a partial class. This brings up one of the best
uses of partial classes. You should use partial classes when you want to write
code for a class which is auto-generated. Since the code for the page class is
generated for you, it is important to keep your code separate. This means that
a partial class is perfect, because it keeps the code physically but not
logically separate. Being in a separate file means the auto-generating process
will not kill your code and the compiler will still know to add in your code
before it compiles the code.
Working with Generated Code
Some developers use software to generate code for them, and
this will let them modify the code for their own purposes without making
changes to the generated code. I use LLBLGen, and it generates a ton of code
for me. When I need to make modifications I can't go in and edit the files it
creates, because the next time I generate the files my changes will disappear.
Instead I create a new file for each class I want to extend. I then add in the
definition for a partial class. This file I have created will not be
overwritten by LLBLGen since LLBLGen didn't create the file, and the compiler
will still make sure that my code is compiled into the class.
Alongside generated code is likely the most common place
where partial classes are used. Since ASP.NET pages use it, and it works so
well it just makes sense to use partial classes with generated code. If you
ever find generated code which is not using partial classes, make sure to
mention to the person, group, or company who made the generator about your
request that the classes be written as partial classes. It will make your code
better organized, easier to work with, and safe from being overwritten.
Multiple Developers Working On the Same Classes
It gets annoying sometimes when writing code with multiple
developers. If the developers will be working without or with minimal source
management this can be very necessary. I often run into problems having to
merge changes with other people. This happens because most developers I know
add new code to the bottom of the file. This is probably because it is easy to
add code there without having to think about placement. It gets annoying after
a while when everyone does this, and sometimes merging tools get confused and I
end up merging by hand anyway. I've even had auto-merge make mistakes when
merging and I've obtained code which does not compile. Sometimes it is easier
to just have partial classes for this. This does add a small maintenance
headache, and you might be thinking that it becomes difficult to find the
correct code. I never have a problem finding the correct code in the file, and
this is because of a little tool we all know and love. It is called "Go To
definition". For those of you who somehow missed this important tool I've
included a nice screenshot. Just right click on the named piece of code and you
will see these options. Click on "Go To Definition".
Figure 1: Go To Definition
I really hope that everyone knows how to do this already. If
you don't know how to use this feature then you should learn it quickly. Your
life will become much better. When you use this you will not need to remember
where the code is because Visual Studio will take you directly to the location
When you're working with multiple people and not using a
versioning system often, it becomes quite difficult to manage the changes
without painful merges. As long as you're not working on tightly knit code, it
is easier to just keep the code in separate files. Sure, files become a little
bit more of a pain to manage, but it is very nice to not have to merge changes
in files after extensive changes in the code.