2007-11-28
| Table of Contents: |
| Rate This Article: | Add This Article To: |
( Page 2 of 2 )
Implementing Partial Methods
Partial methods can be contained in a class or struct that is also partial. (This makes sense because in most cases the declaration will be in one place and the implementation in another but both declaration and implementation are part of the same class.) Partial method declarers can invoke the partial method freely without regard for whether an implementer will actually provide an implementation for the partial method. The compiler takes care of the plumbing for you. If a partial method is declared and used but not implemented then the compiler strips out all calls to the partial method.
The most common use for partial methods will likely be generated code and sub-framework implementers. I almost hate to compare anything to SAP, but partial methods are like ABAP user exits. Basically, SAP consumers are strongly discouraged from customizing SAPs code base. However, SAP does provide function calls called user exits that SAP developers can add custom code to. The benefit is that SAP consumers can write custom code and if SAP replaces or updates their codebase the custom code is left intact.
In reality partial methods are almost identical to delegates. For example, anyone can declare and event property and consumers can choose to implement a handler or not. Partial methods provide roughly the same role. A couple of differences are that delegates are multicast—multiple receivers—and partial methods are not. In C# event properties need to be checked for null before invoking and remain in place whether used or not. Partial methods can be called and the compiler removes non-implemented calls. Listing 1 shows two parts of the same class. The first part has the partial declaration and use of a partial method and the second part provides the implementation of the partial method.
Listing 1: Demonstrating a partial method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PartialMethods
{
class Program
{
static void Main(string[] args)
{
const decimal BMW_750IL = 104000;
Console.WriteLine("Expensive car actual cost: {0:c}", (new PartialClass()).AddSalesTax(BMW_750IL));
Console.ReadLine();
}
}
partial class PartialClass
{
partial void BeforeBehavior(ref decimal amount);
public decimal AddSalesTax(decimal amount)
{
BeforeBehavior(ref amount);
decimal result = amount * (1 + 0.06M);
AfterBehavior(amount);
return result;
}
partial void AfterBehavior(decimal newAmount);
}
partial class PartialClass
{
partial void BeforeBehavior(ref decimal amount)
{
const decimal LUXURY_TAX_RATE = 0.10M;
if (amount > 10000)
amount *= (1 + LUXURY_TAX_RATE);
}
}
}
In the listing there is a partial class named PartialClass. PartialClass defines a method AddSalesTax. (AddSalesTax is hard coded to add Michigan’s current sales tax rate of 6%. In addition there are two partial methods, BeforeBehavior and AfterBehavior. In the other part of the partial class an implementation is provided for BeforeBehavior but not AfterBehavior even thought both are called in AddSalesTax. The implementation of BeforeBehavior simulates adding a luxury tax to very expensive items. In the scenario, then, the already expensive BMW 750IL costs an additional $16,000 dollars which is the government’s cut.
It goes without saying that we wouldn’t generally hard code things—called using magic numbers—like sales tax rate. (In fact, I think Michigan’s rate is tragically going up. Sigh, no beamer for me.)
Some Scenarios for Partial Methods
There are some possible scenarios for partial classes that are known and some of you may be the inventors of others unimagined.
I mentioned that you can use partial methods as placeholders for generated code. Partial methods can also be used as placeholders in frameworks, much like events are used today. Additional scenarios may include dividing code between developers depending on skill levels, or splitting out code that is proprietary for obfuscation.
If you think of some additional uses for partial methods make sure you blog about them.
Summary
Partial methods permit splitting the declaration and the implementation. The most likely scenario to date for partial methods is to provide extension points for people that are consuming generated code. Other scenarios will ultimately be defined, but for now this is a technique that may be only occasionally useful at present yet good to know about.
Biography
Paul Kimmel is the VB Today columnist for HYPERLINK "http://www.codeguru.com/" \t "new" www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his upcoming book LINQ Unleashed for C# due in Spring 2008. You may contact him for technology questions at pkimmel@softconcepts.com.
If you are interested in joining or sponsoring a .NET Users Group, check out www.glugnet.org. Glugnet opened a users group branch in Flint, Michigan in August 2007. If you are interested in attending, check out the www.glugnet.org web site for updates.
![]() |
|


