Partial Methods in C#

Partial Methods in C# with Examples

In this article, I am going to discuss Partial Methods in C# with some examples. Please read our previous article before proceeding to this article where we discussed Partial Classes in C#. At the end of this article, you will understand what exactly Partial Methods are and when and how to use Partial Methods in C#

What are Partial Methods in C#?

A partial class or a struct can contain partial methods. A partial method is created using the same partial keyword.

Let us understand partial methods with an example. Create a console application. Add a class file with name PartialClassOne.cs to the project. Copy and paste the following code. 

namespace PartialDemo
{
    partial class PartialClass
    {
        // Declaration of the partial method.
        partial void PartialMethod();

        // A public method calling the partial method
        public void PublicMethod()
        {
            Console.WriteLine("Public Method Invoked");
            PartialMethod();
        }
    }
}

Notice that the PartialMethod() definition has the partial keyword and does not have a body(implementation) only the signature. The implementation of a partial method is optional. If we don’t provide the implementation, the compiler removes the signature and all calls to the method.

The implementation can be provided in the same physical file or in another physical file that contains the partial class. In this example, the partial PartialMethod() is invoked within the PublicMethod().

Copy and paste the following code in the Main() method of the Program class.
namespace PartialDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            PartialClass SC = new PartialClass();
            SC.PublicMethod();
            
            Console.WriteLine("Press any key to exist.");
            Console.ReadKey();
        }
    }
}
OUTPUT:

Partial Class and Partial Methods in C#

When we run the application, now notice that we don’t get a compiler error, in spite of not having an implementation for the partial PartialMethod(). Since the implementation of the partial method is missing, the compiler will remove the signature and all calls to the method.

Now add a class file with name PartialClassTwo.cs. Copy and paste the following code. The implementation for the partial method is provided here.

namespace PartialDemo
{
    partial class PartialClass
    {
        // Partial method implemented
        partial void PartialMethod()
        {
            Console.WriteLine("Partial PartialMethod  Invoked");
        }
    }
}

Now, run the console application and notice the output. The partial method and the public method messages are printed on the console as shown below.

Partial Methods in C#

So, a partial method declaration consists of two parts. 
  1. The definition (only the method signature ending with a semi-colon, without method body)
  2. The implementation. 

Partial methods are private by default and it is a compile-time error to include any access modifiers, including private. The following code will raise an error stating – A partial method cannot have access modifiers or the virtual, abstract, override, new, sealed, or extern modifiers.

partial class PartialClass
{ 
    private partial void PartialMethod();
}

It is a compile-time error to include declaration and implementation at the same time for a partial method. The code below produces a compile-time error – No defining declaration found for implementing declaration of partial method ‘PartialDemo.PartialClass.partialMethod()’

partial class PartialClass
{
    partial void PartialMethod()
    {
        Console.WriteLine("PartialMethod Implemented");
    }
}

A partial method return type must be void. Including any other return type is a compile-time error – Partial methods must have a void return type

partial class PartialClass
{ 
    partial int PartialMethod();
}

A partial method must be declared within a partial class or partial struct. A non-partial class or struct cannot include partial methods.

Signature of the partial method declaration must match with the signature of the implementation.

A partial method can be implemented only once. Trying to implement a partial method more than once raises a compile-time error – A partial method may not have multiple implementing declarations. 

In the next article, I am going to discuss Sealed Class in C# in Detail. Here, in this article, I try to explain Partial Methods in C# with some examples. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *