C# Partial Method

In c#, the partial method is a special type of method that will contain a declaration part in one partial class and the definition part in another partial class or in the same partial class.

 

Generally, the partial methods will exist either in partial class or struct, and they will contain two main parts one is declaration and definition. Here, the declaration part represents a signature of the partial method, and the definition part represents an implementation of the partial method.

 

In the partial method, the definition part is optional, so it may or may not have an implementation in partial classes. In case the implementation part of the partial method is not available in any partial class. The compiler will remove the method's definition and all the calls related to that particular method in the final class.

 

Following is the example of implementing a partial method using partial keyword in two class files, User1.cs and User2.cs.

User1.cs

public partial class User
{
    // Partial Method Declaration
    partial void GetUserDetails(string a, string b);
    public void TestMethod()
    {
       Console.WriteLine("Test");
    }
}

If you observe the above code, we declared a partial method called GetUserDetails in User1.cs class file using partial keyword along with a standard method called TestMethod().

User2.cs

public partial class User
{
     public User(string a, string b)
     {
         GetUserDetails(a, b);
     }
     // Partial Method Implementation
     partial void GetUserDetails(string x, string y)
     {
        Console.WriteLine("Name: " + x);
        Console.WriteLine("Location: " + y);
     }
}

If you observe the above code, we implemented a partial method called GetUserDetails in User1.cs class file using partial keyword with required variables and constructor.

 

When you execute the above code, the compiler will combine these two partial classes into one User class as shown below.

User

public class User
{
    public User(string a, string b)
    {
        GetUserDetails(a, b);
    }
    public void TestMethod()
    {
        Console.WriteLine("Test");
    }
    private void GetUserDetails(string x, string y)
    {
        Console.WriteLine("Name: " + x);
        Console.WriteLine("Location: " + y);
    }
}

This is how the compiler will combine all the partial classes into a single class while executing the application in the c# programming language.

Rules to Implement Partial Method

In c#, we need to follow certain rules to implement the partial method in our applications.

 

  • In c#, the partial method can be implemented within a partial class or structure only, and the signatures in both parts of the partial type must be the same.
  • The partial method declaration must begin with partial keyword.
  • By default, partial methods are implicitly private, and they cannot be virtual.
  • Partial methods can have ref but not out parameters.
  • Partial methods can be static and generic.

C# Partial Method Example

Following is the example of defining a partial method in a partial class using partial modifier in c# programming language.

 

using System;

namespace Tutlane
{
    public partial class User
    {
        // Partial Method Declaration
        partial void GetUserDetails(string a, string b);
    }

    public partial class User
    {
        public User(string a, string b)
        {
            GetUserDetails(a, b);
        }
        // Partial Method Implementation
        partial void GetUserDetails(string x, string y)
        {
           Console.WriteLine("Name: " + x);
           Console.WriteLine("Location: " + y);
        }
    }

    class Program
    {
       static void Main(string[] args)
       {
           User u = new User("Suresh Dasari", "Hyderabad");
           Console.WriteLine("\nPress Enter Key to Exit..");
           Console.ReadLine();
       }
    }
}

If you observe the above example, we created a partial method GetUserDetails in two partial classes using partial keyword to perform required operations.

 

When you execute the above c# program, we will get the result below.

 

C# Partial Method Example Result

 

This is how we can use partial methods in partial class or structure when we have a method declaration and implementation in multiple files with partial modifier based on our requirements.