Galin Iliev's blog

Software Architecture & Development

C# 3.0 Partial Method Definitions

While I dug into Orcas March CTP Documentation I found another new C# feature (at least for me). It is called Partial Method Definitions and allows you to separate method definition and method implementation in partial classes.


partial void partial void onNameChanged();



partial void onNameChanged()


  //method body



As I have C++ background this sounds familiar to me and reminded me times when I had to declare methods in .h (header) files and put implementation in .c/.cpp files.

To be honest I am not able to find the advantages of this new feature but I suppose there will be more blog post soon that will discuss this.

As I emphasize above you can separate method definition from implementation only in partial classes. There are number of restrictions stated in Orcas documentation:

  • 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 placed 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 not make a delegate to a partial method.


Comments (3) -

  • zproxy

    3/8/2007 7:32:44 PM | Reply

    I infact did not find such a topic in the msdn. Where did you find it?

  • Galcho

    3/9/2007 1:34:52 PM | Reply

    I found them under What's new ... section in Orcas March CTP documentation I installed. It seems this is the first CTP this feature is introduced and there is no much information about it yet.

  • Halo_Four

    3/18/2007 3:07:14 PM | Reply

    Actually the feature wasn't introduced.  It is a feature slated for Orcas but I don't believe it's in the March CTP.

    What I gather of this feature is that it exists as a user extensibility point for designer generated classes.  The designer creates the partial method declaration within it's partial class and calls it within the designer generated code.  The user then has the option to define the implementation of the partial method in their own code in order to be able to act at that point of extensibility.  If the user chooses not to provide an implementation then the compiler removes all traces of the partial method so there is no hit at runtime.

    My understanding is that the designer for LINQ to SQL will use this feature quite a bit.  Pretty much every automatically generated property in the partial class definition has a partial method declaration for before and after that property changes, e.g.

    // In MyEntity.designer.cs
    public partial class MyEntity {
        private string myField;

        public string MyField {
            get { return myField; }
            set {
                OnMyFieldChanging(ref value, ref cancel);
                if(cancel) return;
                myField = value;

        partial void OnMyFieldChanging(ref string value, ref bool cancel);
        partial void OnMyFieldChanged();

    // In MyEntity.cs
    public partial class MyEntity {
        partial void OnMyFieldChanging(ref string value, ref bool cancel) {
            if(value.Length == 0) cancel = true;

    Now I'm guessing as to how it will actually look and work within the LINQ to SQL designer code, but this is what I envision it doing given the little information I've heard.  It seems like a pretty cool mechanism for extending the metaphor of designer generated partial classes while enhancing user extensibility without possibly damaging the designer code.