Partial Methods in C#


A partial class or struct may contain a partial method.

One part of the class contains the signature of the method. An optional implementation may be defined in the same part or another part. If the implementation is not supplied, then the method and all calls to the method are removed at compile time.

The following conditions apply to partial methods:

  • Signatures in both parts of the partial type must match.
  • The method must return void.
  • No access modifiers are allowed. Partial methods are implicitly private.
  • Partial methods can have ref but not out parameters.
  • Partial methods cannot be extern, because the presence of the body determines whether they are defining or implementing.
  • Partial methods can have static and unsafe modifier
  • Partial methods can be generic.
  • You can make a delegate to a partial method that has been defined and implemented, but not to a partial method that has only been defined.

 

Partial methods enable class designers to provide method hooks, similar to event handlers, that developers may decide to implement or not.

The advantage for this is that Visual Studio can provide a graphical designer for part of the class while coders work on the other.




Partial Methods in C#