BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 2.0+

C# Partial Types

C# version 2.0 introduced partial types. This new concept permits the code in a single class, structure or interface to be split into more than one code file. Each constituent part of the type is combined during the compilation process.

Why Use Partial Types?

When using the original version of C# with the .NET framework version 1.1 or earlier, classes, structures and interfaces must each be created within a single file. It is possible to include more than one type per file and to have multiple files containing multiple classes but a single type's code cannot be split into sections in separate files.

In many cases this limitation does not cause a problem. However, when working with very large libraries that cannot be refactored into simpler entities, it can be useful to split the functionality of a single type into several files to allow more than one developer to work on it concurrently. Another drawback is that automatically generated code, such as the InitializeComponent method of a Windows form, must reside with the code created by the programmer. This leads to larger classes with inelegant code hidden by regions.

These problems are solved by the introduction of partial types. A class, structure or interface can be marked as partial to allow its definition to be split between several files. This is often seen in a Windows forms 2.0 application so that the designer-generated code and the programmer's code are held separately. When you review the code associated with a Windows form, you will see that the class is marked as partial.

public partial class Form1 : Form
{
}

To allow the compiler to understand that you are using partial types that must be combined, you must ensure that your classes meet certain rules, in addition to prefixing the type name with the "partial" keyword:

  • All parts of the type must be defined within the same namespace.
  • All parts of the type must be held within the same assembly.
  • If the class inherits from a base class, the base class for all of the parts must match. However, if you omit the base class name from one part it will still inherit from the base class defined elsewhere.
  • No class definition must exist with the same name in the same namespace and assembly without the use of the partial keyword.

Example

To show the use of partial classes, we can create a very simple example. This example will create a single class with two mathematical methods. The class will be split into two code files with a single method in each. To begin, create a new console application and add two classes to the project. Name the class files PartialMath1.cs and PartialMath2.cs.

The two files will each contain half of the class that will be combined during compilation. Modify the declarations for the classes in both files as follows:

public partial class PartialMath

To create the first mathematical method, add the following code to the class within the code file PartialMath1.cs:

public int Add(int a, int b)
{
    return a + b;
}

The second part of the class will be created within PartialMath2.cs. Add the following method to the partial class in that file:

public int Subtract(int a, int b)
{
    return a - b;
}

To demonstrate that the two parts of the class are correctly combined, try executing the following code within the Main method of the program. You should see that the single instance of the PartialMath class contains both of the declared methods.

PartialMath pm = new PartialMath();

Console.WriteLine(pm.Add(15, 10));      // Outputs "25"
Console.WriteLine(pm.Subtract(15, 10)); // Outputs "5"

Considerations

There are some other considerations when creating partial types. Firstly, if any part of a partial class is defined as abstract, every part will be abstract. Secondly, if attributes are applied to one or more parts of a type, every part of the type will be compiled with the same attributes. This may involve combining several attributes that are defined in different parts of the type.

6 November 2008