Developer

Divide functionality into manageable pieces with .NET 2.0's partial classes

A new feature in .NET 2.0 is partial classes, which allow a class file to be defined in multiple files. Learn how to put partial classes to work and how to get the most benefits from them.

The list of enhancements and new features available in .NET 2.0 can be a bit overwhelming. My method has been to wait to dig into the details of the various features until a project calls for it. However, you can use one of the new features, partial classes, throughout your project.

What are partial classes?

Partial classes allow a class to be defined over multiple distinct files. Each file appears to contain a regular class definition. This isn't an object-oriented development feature adopted in .NET; rather, it selfishly (from Microsoft's viewpoint) serves the purpose of addressing issues with Visual Studio .NET.

It makes sense if you're familiar with Visual Studio and how it generates its own code for elements like Web and Windows' Forms. The class file for these elements uses the form name and is contained in the codebehind file. It can be a bit confusing to work with the code, as auto-generated code is mixed with your own.

Visual Studio 2005 takes a different approach by utilizing partial classes. The IDE-generated code is contained in its own class file. Your own code may be added via a separate class file, and other developers can add code via their own partial class files. The compiler handles everything by combining the partial classes into one class during compilation. The resulting intermediate language (IL) from compilation is the same regardless of whether one or multiple class files are used.

Putting partial classes to work

Partial classes are one of the few .NET features that are the same in both VB.NET and C#; the partial keyword denotes a class as such. A quick way to get a look at their declaration is via a Windows Form project created via Visual Studio. Partial classes are used with Windows Forms. The following C# code includes the class file used with an application's default form.

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
} }

Here's the same form created in a VB.NET project:

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
End Sub
End Class
Weekly .NET tips in your inbox
TechRepublic's free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET.
Automatically sign up today!

You may notice the absence of the Visual Studio-generated code for the form. This is where partial classes enter the picture. The generated code is contained in a separate file (partial class) with the file name defined as a form name followed by Designer.vb or Designer.cs depending on the language. So, our sample form has the following partial classes defined to handle the code generated by Visual Studio:

C# - Form1.Designer.cs
VB.NET - Form1.Designer.vb

For demonstration, the source for the Form1.Designer.vb file is in Listing A. It contains the code to set up the visual and any Visual Studio-related elements.

More than Visual Studio friendly


While partial classes make Visual Studio-generated code a little easier to manage, you can also use it in other projects. One of the most popular applications of partial classes is using them to separate different areas of code.

There is no set approach to separating code into partial classes because it depends on the project; however, a common approach is to divide by logical layer. For example, Visual Studio divides it into presentation layer (generated code) and logical layer (custom code). In addition, you may add a data layer. The choice is up to the developer and/or architect.

As a simple example, a custom class is developed for working with employees. The properties will be contained in one class file while methods are in another. Listing B contains the first partial class file (in C#).

The second partial class file follows:

partial class Person1 {
public double EstimatedTaxes() {
return (Salary * 0.20);
} }

Listing C contains the equivalent VB.NET code. Here's the second VB.NET class file:

Partial Public Class Person
Public Function EstimatedTaxes() As Double
Return (Salary * 0.2)
End Function
End Class

This example clearly demonstrates how you can separate code to span across one or more class files. The compiler handles merging everything together.

Another option

The partial class concept is getting mixed reviews. Many developers see it as a hack to support features of Microsoft's own Visual Studio development tool. While this is certainly a valid point, there are other benefits of partial classes. For example, partial classes can streamline the approach of multiple developers working on the same class, as each developer's work may be contained in a partial class and combined during compilation. If you use Visual Studio, partial classes will be used regardless of your wishes, but you will likely find other applications of the feature in current and future projects.

Miss a column?

Check out the .NET Archive, and catch up on the most recent editions of Tony Patton's column.

Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.

About

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

Editor's Picks