Corey Coogan

Python, .Net, C#, ASP.NET MVC, Architecture and Design

  • Subscribe

  • Archives

  • Blog Stats

    • 112,314 hits
  • Meta

Posts Tagged ‘C#’

Partial Methods on Partial Classes

Posted by coreycoogan on June 9, 2009


Here’s something I learned recently while reading Professional ASP.NET 1.0 – not only are there partial classes, but also partial methods.

I can’t believe this feature has been around as long as partial classes and I haven’t heard about it until now [UPDATE 7/10/2009: Thanks to Scott Mitchell’s Comment, where he pointed out that this actually came with the 3.5 Framework].  I must have read 25 blog posts and articles when partial classes were being introduced talking about how cool they were and none of them ever mentioned the awesomeness of partial methods.

Until now, when I wanted to define a method on one side of a generated class and let it be implemented on another, I would create a base class and add a virtual method to be overridden, such as the example below.  Partial methods are private, so this approach still has merit when true inheritance is required.

Old Way

//My generated class
public partial class DataMapper : DataMapperBase
{
     public void Map()
     {
          //foreach loop or something

         //now do custom mapping
         MapCustom();
     }
}

//my custom partial class
public partial class DataMapper
{
     protected override void MapCustom()
     {
         //do custom mapping
     }
}

//generated base class
public abstract class DataMapperBase
{
     protected virtual void MapCustom(){}
}

As I said, this works, but it’s ugly. Now here’s what it looks like with partial methods:

With Partial Methods

//My generated class
public partial class DataMapper
{
     public void Map()
     {
          //foreach loop or something

         //now do custom mapping
         MapCustom();
     }

     //define the partial method
     partial void MapCustom();
}

//my custom partial class
public partial class DataMapper
{
     //implement the partial method
     partial void MapCustom()
     {
         //do custom mapping
     }
}

How do partial methods work you ask? If a partial method gets implemented by any of the other partials, it’s compiled and all is good. If it is not implemented, the compiler removes the method definition, as well as all calls to it, as though it was never there. More details are available on the MSDN site.

There are some limitations, so here’s the rules (from MSDN):

  • 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.

Posted in C# | Tagged: , | 2 Comments »