Dot Net For All

Partial Methods in C# Overview

Why Partial Methods in C# ?

Partial methods in C# have been introduced to customize the type’s behavior.  But how we would have done it in case if there were no partial methods. Suppose we are providing a class whose behavior we want to be customized by the user of the class. We can achieve this functionality by providing a virtual function in the class which can be overridden in the derived class as shown below.

    /// <summary>
    /// Class exposed to be used by third party
    /// </summary>
    public class MyBaseClass
    {
        private string myName;
        public string MyName
        {
            get { return myName; }
            set
            {
                myName = value;
                OnNameChanging();
            }
        }
        public virtual void OnNameChanging()
        {

        }
    }

Now if we want to customize the behavior of this class, we need to derive from this class and override the OnNameChanging() method in the derived class as shown below,

    /// <summary>
    /// Class created by developer of third party to consume the MyBaseClass's functionaliity
    /// </summary>
    public class MyDerivedClass:MyBaseClass
    {
        public override void OnNameChanging()
        {
            Console.WriteLine("MyName is changed");
        }
    }

Now with the above code we can notice that there can be couple of problems which can arise as discussed below:

Using Partial Methods in C#

Partial methods in C# can be used to overcome all these shortcomings and still be used to extend the functionality of the methods of the class as shown in the code snippet provided below.

    ///// <summary>
    ///// Class exposed to be used by third  party
    ///// </summary>
    internal sealed partial class MyBaseClass
    {
        private string myName;
        public string MyName
        {
            get { return myName; }
            set
            {
                myName = value;
                OnNameChanging();
            }
        }

        partial void OnNameChanging();
    }

    /// <summary>
    /// Consumer class definition
    /// </summary>
    internal partial class MyBaseClass
    {
        partial void OnNameChanging()
        {
            Console.WriteLine("On name changing");
        }
    }

Points to note in this newer version of code using partial methods.

Rules for using Partial Methods in C#:

Is it possible to have multiple partial methods to have implementation?

No, it is not possible to have multiple implementation of the partial methods  as it will result into an compile time error as shown below figure.

Conclusion:

In this series about the different type of the methods in C#, I have discussed about Constructors, operator overload methods, extension methods and now in this article partial methods.

With the correct knowledge about all these methods in C# we can make the programming much easier and convenient to be extensible. Please let me know your thoughts about the article.

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview