Understanding C# Features (9) Partial Method

[LINQ via C#] - [C# Features]

The partial keyword

The partial keyword is introduced since C# 2.0. It enables class/struct/interface definition to be split to multiple code files at design time. For example, When creating a WinForm application project in VisualStudio, a form definition 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 when a type has some code implemented by developer, some other code auto generated.

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

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

Here partial method OnValidate can be optionally implemented by developer 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;
        }
    }
}

If implemented this OnValidate method will be invoked when the WebOSUser entity instance is being validated.   

Compilation

Apparently partial method must be declared within a partial class/struct, which can have a method implementation.

Partial method consists of a declaration and an optional implementation. At compile time:

  • If the implementation is not provided, 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 reasons, access modifiers and attributes are not allow on partial method.

For the same 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);
}

8 Comments

  • https://ma-study.blogspot.com/

  • I like your many articles it will help not only me and also many.

  • Electric smoke vapes have become a significant trend among both smokers looking to quit and individuals who have never smoked traditional cigarettes. The market continues to evolve, with new devices and flavors constantly emerging.

  • Your writing is perfect and complete. <a href="https://google.gg/url?sa=t&url=https%3A%2F%2Fwww.mtclean.blog/">safetoto</a> However, I think it will be more wonderful if your post includes additional topics that I am thinking of. I have a lot of posts on my site similar to your topic. Would you like to visit once?

  • As I am looking at your writing, <a href="https://google.ge/url?sa=t&url=https%3A%2F%2Fwww.mtclean.blog/">casino online</a> I regret being unable to do outdoor activities due to Corona 19, and I miss my old daily life. If you also miss the daily life of those days, would you please visit my site once? My site is a site where I post about photos and daily life when I was free.

  • Looking at this article, I miss the time when I didn't wear a mask. <a href="https://google.ga/url?sa=t&url=https%3A%2F%2Fwww.mtclean.blog/">totosite</a> Hopefully this corona will end soon. My blog is a blog that mainly posts pictures of daily life before Corona and landscapes at that time. If you want to remember that time again, please visit us.

  • I have been looking for articles on these topics for a long time. <a href="https://google.fr/url?sa=t&url=https%3A%2F%2Fwww.mtclean.blog/">baccarat online</a> I don't know how grateful you are for posting on this topic. Thank you for the numerous articles on this site, I will subscribe to those links in my bookmarks and visit them often. Have a nice day

  • دانلود انواع جزوه و کتاب در سایت جزوه سیتی

Add a Comment

As it will appear on the website

Not displayed

Your website