Wednesday, May 13, 2009

Partial Classes with C# 2.0

With C# 2.0 it is possible to split definition of classes, interfaces and structures over more than one files.This feature allows you to do a couple of fancy things like:
1- More than one developer can simultaneously write the code for the class.
2- You can easily write your code (for extended functionality) for a VS.NET generated class. This will allow you to write the code of your own need without messing with the system generated code.
There are a few things that you should be careful about when writing code for partial classes:
1- All the partial definitions must precede with the key word "Partial".
2- All the partial types meant to be the part of same type must be defined within a same assembly and module.
3- Method signatures (return type, name of the method, and parameters) must be unique for the aggregated typed (which was defined partially). i.e. you can write default constructor in two separate definitions for a particular partial class.

No comments:

Bookmark and Share