Monday, November 17, 2008

The newly introduced “partial” keyword can be used to split a single class, interface, or struct into multiple, separate files in ASP.NET (using C# as the language). This permits multiple developers to works on these files, which can later be treated as a single unit by the compiler at the time of compilation and compiled into MSIL code. Partial classes can improve code readability and maintainability by providing a powerful way to extend the behavior of a class and attach functionality to it. This article discusses partial classes and how to use them in our ASP.NET applications in a lucid language (with code examples, wherever necessary).

Partial Classes in ASP.NET

Using partial classes helps to split your class definition into multiple physical files. However, this separation does not make any difference to the C# compiler, as it treats all these partial classes as a single entity at the time of compilation and compiles them into a single type in Microsoft Intermediate Language (MSIL). ASP.NET 2.0 uses partial classes as the code-beside class (a new evolution of the code behind model). It is generated by ASP.NET 2.0 to declare all the server side controls you have declared in your ASPX file. The following is an example of a partial class definition:

using System;

using System.Web.UI;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

}

Following are the major benefits of using partial classes:

Isolation of the business logic of an application from its user interface

Facilitates easier debugging

Implementing Partial Classes

We can split one part of a class in one file and the other part in some other file using the partial keyword. The compiler merges these partial classes spread across one or more source files into a single compiled class at the time of compilation — provided all these partial types are available. The following code examples illustrate this better:

Employee1.cs

public partial class Employee

{

public void ReadData()

{

//Some code

}

}

Employee2.cs

public partial class Employee

{

public void SaveData()

{

//Some code

}

}

class Test

{

public static void Main(string[] args)

{

Employee employee = new Employee();

//Some Code

employee.ReadData();

//Some code

employee.SaveData();

}

}

Points to Be Noted

This section discusses some of the most important points that we should keep in mind when working with partial classes or types. Note that all partial types are defined using the keyword “partial”. Further, the “partial” keyword should precede the type signature definition. Note that the partial keyword applies to classes, structs, and interfaces, but not enums. It also does not apply to classes that extend the system classes or types. According to MSDN, “It is possible to split the definition of a class or a struct, or an interface over two or more source files. Each source file contains a section of the class definition, and all parts are combined when the application is compiled”. The partial types for a specific type should be located in the same module or assembly.