Understanding C# 3.0 Features (8) Partial Method

[LINQ via C# series]

The is a very simple feature.

From partial class to partial method

Partial class is introduced by C# 2.0. With the partial keyword, the definition of one type is able to be divided into several files. For example, if creating a WinForm application project in VisualStudio, the MainForm.cs is typically like this:

public partial class MainForm : Form
{
    public MainForm()
    {
        this.InitializeComponent();
    }
}

The InitializeCompoment() method is auto generated in the MainForm.Designer.cs file:

partial class MainForm
{
    #region Windows Form Designer generated code

    private void InitializeComponent()
    {
        this.SuspendLayout();
        // 
        // MainForm
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(282, 255);
        this.Name = "MainForm";
        this.Text = "Form1";
        this.ResumeLayout(false);
    }

    #endregion
}

The partial class improves the productivity in the scenarios that, in one type, some code should be written by programmer, while other code is auto generated and maintained by tool.

Now in C# 3.0 introduces partial methods. For example, In LINQ to SQL dbml, The MSLinqToSQLGenerator generates definition declaration like this:

public partial class WebOSUser : INotifyPropertyChanging, INotifyPropertyChanged
{
    partial void OnValidate(ChangeAction action);
}

So the implementation can be created in another place:

public partial class WebOSUser
{
    partial void OnValidate(ChangeAction action)
    {
        switch (action)
        {
            case ChangeAction.Delete:
                // Validates object when deleting.
                break;

            case ChangeAction.Insert:
                // Validates object when inserting.
                break;
        }
    }
}
Then this OnValidate() method will be invoked when the WebOSUser entity instance is being validated.   

Compilation

It is obvious that partial method must be declared within a partial class / struct. Otherwise it does not make sense.

Partial method consists of a declaration and an optional implementation:

  • If the implementation code is not provided, the compiler removes the definition declaration, and all the invocations;
  • If the implementation is provided, this partial method is compiled into a normal private method.

For the above reason, access modifiers and attributes are not allow on partial method. It is consistently considered private.

For the same above reason, partial method must return void. Otherwise, when implementing declaration is not provided, there is no way to compile or remove the partial method declaration and invocation:

partial int PartialMethod();

private static void Main()
{
    int result = PartialMethod();
    Console.WriteLine(result);
}
Published Wednesday, December 16, 2009 2:00 PM by Dixin

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)