Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Four real world uses of Partial classes and Partial methods

4.83/5 (84 votes)
17 Jan 2012CPOL5 min read 280.2K  
In this article we will explain for real world use of Partial classes and partial methods.

Table of contents

Introduction

Recently I was researching about partial classes and their real world use. Many of the postings found on Google talked about the concept of partial classes and partial methods, but very few highlighted in what scenarios to use them.

In this article, we will first start with the fundamentals of partial classes and methods and then discuss four real world uses. I have also created a video here where I have discussed about partial classes and shown their real world use.

Image 1

Fundamentals of partial classes

A partial class allows a single class to be divided into two separate physical files. During compile time, these files get compiled into a single class. For instance, you can see in the below figure we have the customer class divided into two different files “customer1.cs” and “customer2.cs”.

During compilation, these files get compiled into a single class internally. So when you create an object of the Customer class, you will be able to see methods lying in both the physical files. For instance, you can see the Add method belongs to customer1.cs and the Delete method belongs to customer2.cs, but when the Customer object is created, we can see both the Add and Delete methods.

Image 2

Fundamentals of partial methods

There is one more important concept in partial classes called partial methods. Partial methods helps us to define a method in one physical file and we can implement that method in another physical file, as shown in the below figure.

In the figure, you can see we have defined the Validate method in Customer1.cs and this Validate method is implemented in Customer2.cs. Please note the partial keywords attached to both of these methods.

Image 3

Use number 1: ASP.NET auto generated code

The biggest use of partial classes is in technologies where there is code generation. The Microsoft team themselves use partial classes in ASP.NET, LINQ, and EF code generation. For instance when we look at ASP.NET, there are two parts: the auto generated code of a page and the code-behind where you write your custom logic.

The custom logic is written in the “.aspx.cs” file while the auto generated logic is in the “.aspx.designer.cs” file, as shown in the below figure.

Image 4

As a developer, you would like the auto generated code to do its work, i.e., generate code when you drag and drop a button in the ASP.NET designer.

Image 5

Below is how the auto generated code looks like:

C#
public partial class WebForm1 {
        
    /// <summary>
    /// form1 control.
    /// 
    /// <remarks>
    /// Auto-generated field.
    /// To modify move field declaration from designer file to code-behind file.
    /// </remarks>
    protected global::System.Web.UI.HtmlControls.HtmlForm form1;
    
    /// <summary>
    /// Button1 control.
    /// </summary>
    /// <remarks>
    /// Auto-generated field.
    /// To modify move field declaration from designer file to code-behind file.
    /// </remarks>
    protected global::System.Web.UI.WebControls.Button Button1;
        
    /// <summary>
    /// Label1 control.
    /// </summary>
    /// <remarks>
    /// Auto-generated field.
    /// To modify move field declaration from designer file to code-behind file.
    /// </remarks>
    protected global::System.Web.UI.WebControls.Label Label1;
}

At the same time, you would also like to customize the code in some other file in such a way that the auto generation part is not disturbed. For that, ASP.NET provides the “.aspx.cs” file which is a partial class where you can put your own custom logic.

C#
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Your custom logic
    }
}

This is only possible when the class is split into two physical files but united via the partial keyword. So if you look at any ASP.NET code-behind class file, it’ll be marked with the word partial.

By using the partial keyword in ASP.NET, the Microsoft team has made VS and developers work side by side thus not fiddling with each other’s code and increasing productivity.

Image 6

Use number 2: LINQ and Entity Framework

LINQ and EF also use partial classes and methods heavily because of the auto generation nature of these technologies. So when you drag tables in these framework, they create auto generated classes as shown in the below figure.

In the figure, you can see how the auto generated code has partial classes and partial methods.

Image 7

Image 8

The partial methods later can be extended to put custom logic. For instance, you can see in the below code that for the above auto-generated class tblCustomer, we have used partial methods to override the OnCustomerCodeChanged event to ensure that customer code is not more than 8 characters.

C#
public partial class tblCustomer
{
    partial void OnCustomerCodeChanged()
    {
        if (_CustomerCode.Length > 8)
        {
            throw new Exception("Customer code can not be greater than 8");
        }
    }
}

So by using partial classes and partial methods, LINQ and EF keep auto generating classes and by using partial methods, we can customize the classes with our own logic.

Use number 3: Better maintenance by compacting large classes

The other important use of partial classes is for better maintenance of the project. If you have large classes with lots of methods as shown in the figure, it’s a bit of a pain to maintain those classes.

Image 9

By using partial classes, you can split them into physical files as shown in the below figure, thus making your project better and easy to maintain.

Image 10

Use number 4: Multiple people working on the same class

Image 11

The last and final real world use I see of partial classes is when we want developers to work simultaneously in the same class. I agree this can be a very rare use as there are better options like using a version control software like TFS or Subversion, but in case you want something quick and local, this option is not bad at all.

For further reading do watch the below interview preparation videos and step by step video series.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)