Partial Classes in ASP.NET 2.0

Improve Code Readability and Maintainability

Joydip Kanjilal

October 30, 2009

3 Min Read
ITPro Today logo in a gray background | ITPro Today

asp:Feature

LANGUAGES:C#

ASP.NETVERSIONS: 2.0

 

Partial Classes in ASP.NET 2.0

Improve Code Readability and Maintainability

 

By Joydip Kanjilal

 

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

 

Partial Classes in ASP.NET

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

 

using System;

using System.Web.UI;

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

{

   protected void Page_Load(objectsender, EventArgs e)

   {

   }

}

 

Following are the major benefits of using partial classes:

  • Isolation of the business logic of anapplication from its user interface

  • Facilitates easier debugging

 

Implementing Partial Classes

We can split one part of a class in one file and the otherpart in some other file using the partial keyword. The compiler merges thesepartial classes spread across one or more source files into a single compiledclass at the time of compilation provided all these partial types areavailable. 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 = newEmployee();

   //Some Code

   employee.ReadData();

   //Some code

   employee.SaveData();

 }

}

 

Points to Be Noted

This section discusses some of the most important pointsthat we should keep in mind when working with partial classes or types. Notethat all partial types are defined using the keyword partial . Further, the partial keyword should precede the type signature definition. Note that the partialkeyword applies to classes, structs, and interfaces, but not enums. It alsodoes not apply to classes that extend the system classes or types. According toMSDN, It is possible to split the definition of a class or a struct, or aninterface over two or more source files. Each source file contains a section ofthe class definition, and all parts are combined when the application iscompiled . The partial types for a specific type should be located in the samemodule or assembly.

 

Conclusion

This article has discussed partial classes in ASP.NET 2.0,how they can be used, and their benefits. I welcome readers comments andsuggestions. Happy reading!

 

References

Please refer to the following links for further referenceon this topic:

 

Working extensively in Microsoft technologies for more than 10years, Joydip Kanjilal is a SeniorProject Leader for a company in Hyderabad, India.His programming skills include C, C++, Java, C#, VB, VC++, ASP.NET, XML, andUML. He has worked with .NET and C# for more than five years. Reach Joydip at mailto:[email protected].

 

 

 

 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like