How are partial methods used in C# 3.0?


I have read about partial methods in the latest C# language specification, so I understand the principles, but I'm wondering how people are actually using them. Is there a particular design pattern that benefits from partial methods?

9/4/2008 12:00:35 PM

Accepted Answer

Partial methods have been introduced for similar reasons to why partial classes were in .Net 2.

A partial class is one that can be split across multiple files - the compiler builds them all into one file as it runs.

The advantage for this is that Visual Studio can provide a graphical designer for part of the class while coders work on the other.

The most common example is the Form designer. Developers don't want to be positioning buttons, input boxes, etc by hand most of the time.

  • In .Net 1 it was auto-generated code in a #region block
  • In .Net 2 these became separate designer classes - the form is still one class, it's just split into one file edited by the developers and one by the form designer

This makes maintaining both much easier. Merges are simpler and there's less risk of the VS form designer accidentally undoing coders' manual changes.

In .Net 3.5 Linq has been introduced. Linq has a DBML designer for building your data structures, and that generates auto-code.

The extra bit here is that code needed to provide methods that developers might want to fill in.

As developers will extend these classes (with extra partial files) they couldn't use abstract methods here.

The other issue is that most of the time these methods wont be called, and calling empty methods is a waste of time.

Empty methods are not optimised out.

So Linq generates empty partial methods. If you don't create your own partial to complete them the C# compiler will just optimise them out.

So that it can do this partial methods always return void.

If you create a new Linq DBML file it will auto-generate a partial class, something like

public partial class MyDataContext : System.Data.Linq.DataContext

    partial void OnCreated();
    partial void InsertMyTable(MyTable instance);
    partial void UpdateMyTable(MyTable instance);
    partial void DeleteMyTable(MyTable instance);


Then in your own partial file you can extend this:

public partial class MyDataContext
    partial void OnCreated() {
        //do something on data context creation

If you don't extend these methods they get optimised right out.

Partial methods can't be public - as then they'd have to be there for other classes to call. If you write your own code generators I can see them being useful, but otherwise they're only really useful for the VS designer.

The example I mentioned before is one possibility:

//this code will get optimised out if no body is implemented
partial void DoSomethingIfCompFlag();

//this code won't exist if the flag is off
partial void DoSomethingIfCompFlag() {
    //your code

Another potential use is if you had a large and complex class spilt across multiple files you might want partial references in the calling file. However I think in that case you should consider simplifying the class first.

5/23/2017 12:17:59 PM

Partial methods are very similar in concept to the GoF Template Method behavioural pattern (Design Patterns, p325).

They allow the behaviour of an algorithm or operation to be defined in one place and implemented or changed elsewhere enabling extensibility and customisation. I've started to use partial methods in C# 3.0 instead of template methods because the I think the code is cleaner.

One nice feature is that unimplemented partial methods incur no runtime overhead as they're compiled away.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow