Thursday, August 19, 2010

Partial Class in C#

Partial class:


It is possible to split the definition of a class 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.

Using the partial keyword indicates that other parts of the class, struct, or interface can be defined within the namespace. All the parts must use the partial keyword. All of the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public, private, and so on.

Eg;

using System;
partial class Test
{
private int x;
}


partial class Test
{
public Test()
{
x = 10;
}
}


partial class Test
{
public void Print()
{
Console.WriteLine("X is : {0}", x);
}
}

class Mainclass
{
public static void Main()
{
Test obj = new Test();
obj.Print();
}
}

No comments: