A partial method allows us to separate method definition and implementation. It does not mean that it would allow us multiple implementations. Only a partial class or struct may contain a partial method. One part of the partial class or struct has only declaration and another part of the same partial class or struct may have implementation for that. We can have both in the same part of the partial class or struct. A user may or may not implement the method. A partial method gets executed only when it has an implementation. If the user has not implemented them, the compiler does not include them in the final code.

Rules for Partial Methods:

Example:

  1. namespace TestPartialMethods
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. Student obj = new Student();
  8. Console.ReadLine();
  9. }
  10. }
  11. public partial class Student
  12. {
  13. public Student ()
  14. {
  15. Admission() ;
  16. }
  17. // A partial method definiton
  18. partial void Admission();
  19. }
  20. public partial class Student
  21. {
  22. // A partial method implementation
  23. partial void Admission()
  24. {
  25. Console.WriteLine("Inside implementation");
  26. }
  27. }
  28. }
Partial methods are useful to customize the code generated by any tool. The generated code may have some partial methods. Implementation of these methods is decided by the developers. If developer decide not to implement then compiler would removes them in the final code. It can also be helpful to distribute the development task among the team members.