Monday, April 15, 2013

Partial Methods - C# 3.0

I was going through MSDN site and I came to know that there is a new features of C# 3.0 called Partial Methods.


Partial methods enable the implementer of one part of a class to define a method, similar to an event. 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. 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.
Partial methods are especially useful as a way to customize generated code. They allow for a method name and signature to be reserved, so that generated code can call the method but the developer can decide whether to implement the method. Much like partial classes, partial methods enable code created by a code generator and code created by a human developer to work together without run-time costs.


See the below example :

TestClass1
{

   partial void DoSomething();

}

Testclass2
{
   partial void DoSomething()
   {
      // Some Code
   }
}

No comments:

Post a Comment

Software Development Blogs - BlogCatalog Blog Directory RSS Search Technology Blogs - Blog Rankings Blog Directory