Monday, September 13, 2004

Peek .NET 2005: Partial Classes...

One of the newest addition to the feature list of .NET 2005 is the concept of partial classes. Though it might not be a very exciting feature compared to the rest of the features .NET 2005 has to offer it sure does have its own list of benefits.So what is a partial class?A partial class is a partial definition of a class with the complete definition split across multiple files of class definitions. For example I can define a Customer class split across class definitions in VB.NET(2005) as:
Public Class Customer
Private CustName as String
End Class
Partial Public Class Customer
Private CustBalance as Double
End Class
So once i compile I'll have one single Customer class with two fields(CustName and CustBalance). But why would i want to do that?Why can't i just define both the fields in one Customer class definition?The actual benefit of partial class will be visible when you use code generation.Code generation usually involves your classes generated and your class definitions updated frequently.When you use VS.NET 2003 you might have noticed the Windows Forms Generated Code which is hidden by a region.So a Form in VS.NET 2003 has code that is generated as well as code written by the developer in the same file.With VS.NET 2005 the Form definition will be split using partial classes with one being generated and the other one being coded by the developer.This provides a clean seperation between the generated code and the code written by the developer.
As i said in my previous blog, code generation is a very important thing in current software development practice that shouldn't be ignored.And partial class complement code generation to integrate it into your development stream easily.

No comments: