In C# 3.0, partial classes allow you to split the definition of a class across multiple files. In the same way we can split the definition of a code unit separated over multiple files by Partial Method.
The following are some rules that apply to partial methods.
• Partial methods must only be defined and implemented in partial classes
• Partial methods must specify the partial modifier
• Partial methods are private but must not specify as private modifier
• Partial methods must return void
• Partial methods may be unimplemented
• Parital methods may be static
• Partial methods may have arguments
Example :
public partial class MyClass
{
partial void MyMethodStart(int count);
partial void MyMethodEnd(int count);
public MyClass()
{
int count = 0;
MyMethodStart(++count);
Console.WriteLine("Calling constructor.");
MyMethodEnd(++count);
Console.WriteLine("Count is {0} after calling partial methods. ", count);
}
}
public partial class MyClass
{
partial void MyMethodStart(int count)
{
Console.WriteLine(" MyMethodStart(Count is {0}) .", count);
}
partial void MyMethodEnd(int count)
{
Console.WriteLine("MyMethodEnd(Count is {0}) ", count);
}
}
OUTPUT :
MyMethodStart(Count is 1)
Calling constructor.
MyMethodEnd(Count is 2)
Count is 2 after calling partial methods.
Share your thoughts with me !!!
- Rangoli
The following are some rules that apply to partial methods.
• Partial methods must only be defined and implemented in partial classes
• Partial methods must specify the partial modifier
• Partial methods are private but must not specify as private modifier
• Partial methods must return void
• Partial methods may be unimplemented
• Parital methods may be static
• Partial methods may have arguments
Example :
public partial class MyClass
{
partial void MyMethodStart(int count);
partial void MyMethodEnd(int count);
public MyClass()
{
int count = 0;
MyMethodStart(++count);
Console.WriteLine("Calling constructor.");
MyMethodEnd(++count);
Console.WriteLine("Count is {0} after calling partial methods. ", count);
}
}
public partial class MyClass
{
partial void MyMethodStart(int count)
{
Console.WriteLine(" MyMethodStart(Count is {0}) .", count);
}
partial void MyMethodEnd(int count)
{
Console.WriteLine("MyMethodEnd(Count is {0}) ", count);
}
}
OUTPUT :
MyMethodStart(Count is 1)
Calling constructor.
MyMethodEnd(Count is 2)
Count is 2 after calling partial methods.
Share your thoughts with me !!!
- Rangoli