asp.netPRO The .NET and Java component publisher



Subscription Services
Print Subscription
Online-Only Subscription
Renew Subscription
asp.netNOW Newsletter
Change of Address
Pay An Invoice
Subscription Packages

asp.netPRO
Articles
411asp.net Directory
New Products
Book Reviews
Blog Listings  
E-Newsletter Articles- NEW
Events  - NEW 
Job Listings  
Product Reviews
Opinion
Back Issues
Reprints/E-prints
Search

Downloads
Premium Downloads


Informant
Contact Us
Advertise with Us
Write For Us



 
 
 


Fully Loaded Windows Servers


.NET Pro VPS 2008 Fall Conference in Las Vegas
2007 asp.netPRO Complete Works CD
Co-Sponsored by:
Download your free trial now!


Click here for the online product directory, asp.netPRO Product Portal

 

Latest Features

 •

On-demand XAML: Reloaded


 •

Build a Message Box the AJAX Way: Part I


 •

Starting Your Business


 •

FTP Transfers


 •

More on Dynamic Data



Article Rating



Tell a friend
about this article!




asp:Feature

LANGUAGES: C#

ASP.NET VERSIONS: 2.0

 

Partial Classes in ASP.NET 2.0

Improve Code Readability and Maintainability

 

 

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.

 

Conclusion

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

 

References

Please refer to the following links for further reference on this topic:

 

Working extensively in Microsoft technologies for more than 10 years, Joydip Kanjilal is a Senior Project Leader for a company in Hyderabad, India. His programming skills include C, C++, Java, C#, VB, VC++, ASP.NET, XML, and UML. He has worked with .NET and C# for more than five years. Reach Joydip at mailto:joydipkanjilal@yahoo.com.

 

 

 

 

Microsoft Internet Explorer
Top of page

 

The .NET and Java component publisher

Informant Communications Group

Informant Communications Group, Inc.
5105 Florin Perkins Road
Sacramento, CA 95826
Phone: (916) 379-0609 • Fax: (916) 379-0610

Copyright © 2008 Informant Communications Group. All Rights Reserved. • Site Use Agreement • Send feedback to the Webmaster • Important information about privacy