Partial Methods
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. Partial methods enable the implementer of one part of a class to define a method and the implementer of the other part of the class can decide whether to implement the method or not. If the method is not implemented, then the compiler removes the method signature and all calls to the method. The calls to the method, including any results that would occur from evaluation of arguments in the calls, have no effect at run time. Therefore, any code in the partial class can freely use a partial method, even if the implementation is not supplied. No compile- time or run-time errors will result if the method is called but not implemented.
A partial method declaration consists of two parts: the definition, and the implementation. These may be in separate parts of a partial class, or in the same part. If there is no implementation declaration, then the compiler optimizes away both the defining declaration and all calls to the method.
partial void TestMethod(); // Definition in file1.cspartial void TestMethod() { // Implementation in file2.cs
// method body
}
using System; namespace OOPSProject { partial class PMethod { partial void TestMethod(); public void Show() { Console.WriteLine("Show Method."); } } } using System; namespace OOPSProject { partial class PMethod { partial void TestMethod() { Console.WriteLine("Partial Method Implemented."); } public void s() { Console.WriteLine("Display Method."); } static void Main() { PMethod obj = new PMethod(); obj.Show(); obj.Display(); obj.TestMethod(); Console.ReadLine(); } } }