How to Use Partial Classes in Visual Studio 2005

Partial classes permit splitting a single class, interface, or struct into multiple, separate files. There are several advantages to splitting these elements into many files, including permitting more than one developer to work on the same class. Paul Kimmel shows where partial classes are used and how you might use partial keywords to fine-tune your productivity.
This article is adapted from Paul Kimmel's book C# for the Professional Programmer (Addison Wesley).

From the author of

From the author of

Introduction

The partial keyword was introduced in version 2.0 of the .NET framework. Very
simply, the partial keyword permits you to split the definition of a single
class, structure, or interface into more than one file. This means that if
you’re working on large projects, multiple developers can work on multiple
files, all containing parts of the same class. The partial keyword also means
that it’s possible to split generated code into two parts: the part
that’s generated, and another part that provides a convenient location for
you and me to add customizations.

NOTE

A pessimist might wonder if the partial keyword was invented because
Microsoft’s Visual SourceSafe doesn’t support multiple simultaneous
checkouts of the same file and isn’t known to merge file differences with
eloquence and grace. Whether intended or not, a nice benefit of partial classes
is that SourceSafe’s tolerable deficiencies are somewhat mitigated.

You’ll find the partial keyword used in Windows Forms classes to split
the part that’s managed by the designer and the part where you add your
code into two separate files. Strongly typed datasets use the partial keyword,
permitting you to separate generated code and custom code in case you have to
regenerate the typed dataset at a later time. Partial classes are also used in
XML web services in .NET 2.0.

After you read the rules for using the partial keyword, see if you can come
up with some new uses for partial classes.