C# 3.0 New Feature for the Week #6: Partial Methods

December 22, 2007

tags: ,

This is a feature I was completely unaware of until I started digging into Linq-to-SQL recently. Upon using the Linq class designer on the Northwind database, I looked into the generated code, and saw something like this:

public partial class Category { partial void OnCreated(); public Category() { //Do stuff OnCreated(); } }

This is generated code for the Category entity, and you can see it includes a method definition for OnCreated, which does not have any body and is marked with the partial keyword. Now, if I wanted to add logic to the OnCreated method, I could do that in the second part of the partial class.

public partial class Category { partial void OnCreated() { Console.WriteLine("Category was created"); } }

Since I can’t actually change the generated code (as it might get re-generated, while deleting my changes), the partial method syntax provides a hook for me to enter my logic into the class construction, and put it in a different file.

How is it implemented?

In order to understand what’s going on, we should look at the Category class in the reflector, before and after we add the second partial (the one that implements “OnCreated”).


public class Category { }

And After:

public class Category { // Methods public Category() { this.OnCreated(); } private void OnCreated() { Console.WriteLine("Category was created"); } }

As you can see, if we don’t implement OnCreated, the method isn’t even there! The compiler simply ignores its existence, so you won’t see neither the method definition or any of the method calls. Also note that the method OnCreated is private. Partial methods always are – you cannot add access modifiers (public, protected) to them.

A question comes to mind: why not use events instead? Well, you could define a private OnCreated event, but a private event is a rather weird beast, isn’t it? Since when a class needs events that no one else will get to look at? Not to mention that you will have to remember to register to that event. Also, consider that there might be a whole bunch of hooks such as OnCreated (and there are in the Linq-to-SQL generated code). Most of them will never get used, so why should they even appear in the class? Ignoring them completely makes the class a lot more lightweight.

All in all, this could be a pretty useful tool for all of you out there that generate code. On a side note: Man, I wish I could change the compiler every time I feel like I’m missing a feature. Those lucky Microsoft guys…

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



  1. Yosi TaguriDecember 22, 2007 ב 21:43

    nice post.
    What’s the order of the code being produced.
    Assume I have 3 files with 3 implementations for the same partial method.

  2. doronyDecember 23, 2007 ב 20:53

    You can’t have more than one implementation for the same partial method, the code won’t compile (It’s the same as declaring the same method several times inside one class).

  3. Anonymous Boo FanJanuary 7, 2008 ב 13:44

    If you wish that you could change the compiler any time that you want. Be one of those lucky Boo guys… http://boo.codehaus.org/