Geeks With Blogs

Rajesh Pillai My experiments with mvc, jquery, wpf, silverlight, sharepoint, tdd and design patterns.

c# 3.0 : Partial Method

You are all aware of “Partial Class” introduced in .net 2.0.    Partial class is useful in situation where you need to split the definition of a class,
struct or an interface over two or more source files.  Each source file contains a section of the type or method definition and all parts are
combined when the application is compiled. 

There are several situations when splitting a class definition is desirable:

When working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time.

·          When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio
uses this approach when it creates Windows Forms, Web service wrapper code, and so on. You can create code that uses these classes without
having to modify the file created by Visual Studio.

Partial Method (.net 3.0):

 Partial methods are the code block which reside inside a partial type and gets executed only when it has definition. This gives us the extensibility
and if user wants to implement the rule, they can go ahead and define the body but if they do not want it will not. This improves the performance as
you are not loading/creating unwanted methods. 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.

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.

// Definition in file1.cs

partial void onNameChanged(); 

// Implementation in file2.cs

partial void onNameChanged()
  // method body

  • Partial method declarations must begin with the contextual keyword partial and the method must return void.
  • Partial methods can have ref but not out parameters.
  • Partial methods are implicitly private, and therefore they cannot be virtual.
  • 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 modifiers.
  • Partial methods can be generic. Constraints are put on the defining partial method declaration, and may optionally be repeated on the implementing one. Parameter and type parameter names do not have to be the same in the implementing declaration as in the defining one.
  • 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.

The above knowledge may seem very miniscule but it opens up a whole new opportunity when you are designing your system. 
For e.g.  Microsoft Entity Framework uses this feature for notifying property changes.

But beware there are caveats to it. 

How can this knowledge be applied?  Say for e.g. you have a class called “Address” which is declared as follows

public partial class Address
      private string city;
      public string City
             get { return;}
                     //  If this method is implemented then the actual code will be executed else this will be ignored.
                     this.OnCityChanging(value);     //  This is partial method.  No exception will be thrown if this method is not implemented.                                                                

            = value;

    partial void OnCityChanging (string city);

public partial class Address
      partial void OnCityChanging(string city)
                // You can add business rules/validation or constraints here…
               If (city == “prohibited city”)
                      throw new InvalidArgumentException(“This city is not allowed”);


Create an instance of Address…


Address add = new Address();
add.City = “prohibited city”;   // exception will be thrown.

This is just a rudimentary example to wet up your appetite. Now, wear your “Thinking Hat” and ponder over when you get some time
as to where this can be applied.



Posted on Saturday, November 28, 2009 11:41 AM , c# | Back to top

Comments on this post: Tech Tips: C# 3.0 Partial Methods

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Rajesh Pillai | Powered by: