Partial Method

Partial method is a special type of method which is declared and defined in two separate partial classes. Declaration part represents signature and presents in a partial class. The definition part provides implementation of the method and resides in separate partial class.

The concept of partial method is helpful when we have declaration and definition of method in two separate files. If definition (implementation) of method is not provided, compiler removes the signature at compile time.

There are some rules and restrictions apply to the partial method.

  • The signature of partial method must be same in both partial classes.
  • The partial method must return void.
  • No access modifiers are allowed.
  • Partial methods are implicitly private.

Let's see an example that implements the partial method. This example includes two partial classes. One contains declaration part and second contains definition of the method.

C# Partial Method Example

Example

using System;
namespace CSharpFeatures
{
    partial class A
    {
        // Declaring partial method
        partial void ShowMSG(string msg);
    }
    partial class A
    {
        // Implimenting partial method
        partial void ShowMSG(String msg)
        {
            Console.WriteLine(msg);
        }
        public static void Main(string[] args)
        {
            // Calling partial method.
            new A().ShowMSG("Welcome to the C# Tutorial");
        }
    }
}

Output

Output

Welcome to the C# Tutorial

Input Required

This code uses input(). Please provide values below: