About Me

My photo
a Dynamic and Energetic guy.....

Friday, December 4, 2009

Partial Methods in C#

This is a cool feature that comes with C#.
We can define a method & can omit the logic of that method, that is called Partial method.

If the implementation is not supplied, then the method and all calls to the method are removed at compile time.
This is useful as a way to customize generated code & the developer can work of same method without run-time problems. Because it is separated. We can validate data before it executes using these types of partial methods.

* Partial method should inside Partial Class
  • Partial method declarations must begin with the contextual keyword partial and the method must return void.

  • Partial methods can have ref but not out parameters.

  • Partial methods are implicitly private , and therefore they cannot be virtual.


// Code-generating tool defines a partial class, including
// two partial methods.
partial class ExampleClass
{
partial void onFindingMaxOutput();
partial void onFindingMinOutput();
}

// Developer implements one of the partial methods.
// Compiler
discards the signature of the other method.
partial class ExampleClass
{
partial void onFindingMaxOutput()
{
Console.WriteLine("Maximum has been found.");
}
}

No comments:

My Masters