17 March 2013

partial methods in .net c# (page 1 of 2)

Introduction to partial methods:

I knew about partial classes, but I was not aware of Partial methods in .NET until recently. One of my friends recently attended an interview and while discussing the questions I came to know the existence of partial methods. Thinking along the same lines of partial classes, I questioned him hardly how partial methods can exist. How can partial methods be sequenced? that means, which partial methods implementation will get executed first and which is later?

Partial methods declaration:

Below program shows partial methods inside a partial class

namespace Partial_Method_Example
{
 class Program
 {
   static void Main(string[] args)
   {
    Partial_Method_Illustration partial_class_intance = 
             new Partial_Method_Illustration();
    partial_class_intance.InvokePartialMethod();
            Console.ReadLine();
    }
  }

  public partial class Partial_Method_Illustration
  {
    partial void EventHandler(object arguments)
    {
      Console.WriteLine("From partial method");
    }
   }

   public partial class Partial_Method_Illustration
   {
     public void InvokePartialMethod()
       {
         // Do something else

         // Call partial method
            EventHandler(new object());

         // Do something else
        }
        partial void EventHandler(object arguments);
    }
}

Highlights on the partial methods example program:

  • Partial class "Partial_Method_Illustration" is present twice.
  • Partial method "EventHandler" is present. Partial method is just declared in one partial class and invoked from another methods in the class.
  • Actual implementation of the partial method exists in another partial class.

Why partial methods are necessary?

When code generator tool used, it's not wise to edit the code generated. Because next time when something is updated in code generator tool, it overwrites any changes made manually. To overcome this, partial methods are introduced in .NET 3.0 version. Using the partial method you can hook up something. The code generator tool can just invoke the partial method. The implementation details can be provided by the developer inside other partial class. This way code generator lets the developer modify the code which will not get overridden if the code is updated again in the designer.

This can easily be achieved using a delegate. But it will have a runtime overhead if a developer doesn't provide an implementation in the partial method. But if partial methods are used and the developer has not provided an implementation in the partial method, during compilation the invocation of partial method will be removed. So no runtime overhead of invoking the partial method if the developer has not provided an implementation.

Facts about partial methods:

  • Partial methods can only be used in partial classes.
  • Partial methods cannot return anything. i.e void return type.
  • Can not have access specifier for partial methods. They are implicitly private.
Second page >>