I understand completely how to write a partial method. My question is about the main purpose or the added value when we write a partial method in our C# program.
Thank you all.
I understand completely how to write a partial method. My question is about the main purpose or the added value when we write a partial method in our C# program.
Thank you all.
From Jon Skeet's C# in Depth 3rd Edition:
In summary, partial methods in C# 3 allow generated code to interact with hand- written code in a rich manner without any performance penalties for situations where the interaction is unnecessary. This is a natural continuation of the C# 2 partial types feature, which enables a much more productive relationship between code generators and developers.
Here is some comments from msdn documentation about partial Methods.
Partial methods enable class designers to provide method hooks, similar to event handlers, that developers may decide to implement or not. If the developer does not supply an implementation, the compiler removes the signature at compile time. The following conditions apply to partial methods:
Example:
partial class A
{
partial void OnSomethingHappened(string s);
}
// This part can be in a separate file.
partial class A
{
// Comment out this method and the program
// will still compile.
partial void OnSomethingHappened(String s)
{
Console.WriteLine("Something happened: {0}", s);
}
}
from the reference msdn