Thursday, October 26, 2006

Partial Classes???

Partial Classes, what are they ? are they New ?

Yes CLR2.0 provides the new concept of Partial class---- A single class can be split in to many classes in same name , but with the indicator partial.

Partial classes are a new feature to the .NET Framework 2.0 and again C# takes advantage of this addition.
Partial classes allow you to divide up a single class into multiple class files, which are later combined into a single class when compiled.
To create a partial class, you simply need to use the partial keyword for any classes that are to be joined together with a different class.
The partial keyword precedes the class keyword for the classes that are to be combined with the original class.

For instance, you might have a simple class called Calculator as shown here:

public class Calculator{

public int Add(int a, int b) {
return a + b;
From here, you can create a second class that attaches itself to this first class as shown here in the following example:

public partial class Calculator{

public int Subtract(int a, int b) {

return a - b;

When compiled, these classes will be brought together into a single Calculator class instance as if they were built together to begin with.

1 comment:

Anonymous said...

what if you try to add a method to the partial class with the same name as an already existing one?
It throws a compile errror eh? And only one of the partial classes can have main method?