Partial Method in C# with Examples

  By : Suresh Dasari
  Posted On : 24-Apr-2023

Here we will learn what is a partial method in c#, uses of partial methods, and partial method implementation in c# with examples.

What is the Partial Method?

In c#, the partial method is a method that is defined inside a partial class with a partial keyword. The partial method will contain a method declaration (signature) in one partial class and the implementation of a partial method in another partial class or in the same partial class.

 

In c#, we are allowed to create partial methods only in partial classes or partial structures. The partial methods will contain two main parts declaration and definition. The declaration part will represent a signature of the partial method and the definition part will represent an implementation of the partial method.

 

The following diagram will illustrate the implementation of partial methods in c#.

 

C# Partial Method Implementation Diagram

Partial Method Syntax

As discussed, we can create partial methods within partial classes or structures using partial keyword. Following is the syntax of defining the partial method in c#.

 

//File1.cs
public partial class User
{
   // Partial method declaration
   partial void GetUserDetails();
}

//File2.cs
public partial class User
{
   // Partial method definition
   partial void GetUserDetails()
   {
      //Implementation here
   }
}

In this partial method syntax, we declared GetUserDetails partial method using partial keyword in one partial class (User). The partial method (GetUserDetails) implementation is defined in another partial class.

 

During the compilation time, the compiler will combine partial classes (File1.csFile2.cs) into a single User class as shown below.

 

public class User
{
   private void GetUserDetails()
   {
      //implementation here
   }
}

In c#, by default partial methods are private and the return type is void. If you want to use a different return type int, string, etc. other than void, you need to specify the access modifier for the partial method otherwise you will get the “partial method must have accessibility modifiers because it has a non-void return type” exception as shown below.

 

C# partial methods with access modifiers exception

 

The following is an example of defining the partial methods with int return type in c#.

 

//File1.cs
public partial class User
{
   // Partial method declaration
   public partial int GetUserDetails();
}

//File2.cs
public partial class User
{
   // Partial method definition
   public partial int GetUserDetails()
   {
     return 10;
   }
}

While defining the partial methods, you need to make sure that the signature of the partial method must be same in both declaration and definition parts otherwise we will get the “No defining declaration found for implementing declaration of partial method” exception as shown below.

 

C# partial methods with different signatures

 

Following is the example of defining the partial methods with the same signature in c#.

 

//File1.cs
public partial class User
{
   // Partial method declaration
   partial void GetUserDetails(string a, string b);
}

//File2.cs
public partial class User
{
   // Partial method definition
   partial void GetUserDetails(string a, string b)
   {
      Console.WriteLine(a);
      Console.WriteLine(b);
   }
}

In c#, the definition (implementation) part of partial methods is optional, so sometimes the implementation of the partial method is not available in other parts of the partial class. In that case, the compiler will remove any calls to that method will be removed from the final class during the compilation time.

C# Partial Method Example

Following is the example of implementing the partial methods in a partial class using partial keyword in c#.

 

using System;

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

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

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

In this partial method example, we created GetDetails partial method in User partial class using the partial keyword. In the first part of the partial class, we defined the GetDetails method, and its implementation was defined in another part of the partial class.

 

When you execute this example, you will get the result below.

 

C# Partial Method Example Result

C# Partial Method without Implementation

Can we create a partial method without implementation? The answer is YES. We can create partial methods without implementation, it’s not mandatory to have implementation for the partial method.

 

If we create partial methods without implementation, those methods will be removed during the compilation time.

 

public partial class User
{
   // Partial Method Declaration
   partial void GetDetails();
}

public partial class User
{
   public User(string name, string location)
   {
      GetUserDetails(name, location);
   }

   // No Implementation of GetDetails Method
}

In this example, we have a declaration for the GetDetails method but the implementation of the GetDetails method is not available. In this case, the compiler will remove the GetDetails method call references from the final class during the compilation time.

C# Partial Method with Multiple Implementations

Can we create a partial method with multiple implementations? The answer is NO. If we create a partial method with multiple implementations, we will get the “A partial method may not have multiple implementing declarations” exception as shown below.

 

C# partial method with multiple implementations example

C# Partial Method Return Value

Will the partial method return a value in c#? The answer is YES. By using partial methods, we can return the value of whatever we want to return.

 

Following is the example of returning the value using the partial method in c#.

 

//File1.cs
public partial class User
{
   // Partial method declaration
   public partial int GetUserDetails();
}

//File2.cs
public partial class User
{
   // Partial method definition
   public partial int GetUserDetails()
   {
      return 500;
   }

Rules to Implement Partial Methods in C#

While working with partial methods in c#, remember the following rules.

 

  1. In c#, we need to create the partial methods within a partial class or partial structure using partial keyword.
  2. The partial method should contain a method declaration (signature) in one partial class and the implementation of the partial method in another partial class or in the same partial class.
  3. By default, partial methods are private, and the return type is void. If you want to use different return types int, string, etc. other than void, you need to specify the access modifier for the partial method.
  4. While defining the partial methods, you need to make sure that the signature of the partial method must be same in both declaration and definition parts.
  5. In c#, the partial method must be implemented only once either in the same part of the partial class or other parts of the partial class.
  6. In c#, partial methods can have ref parameters and those can be static.

This is how we can create and use partial methods in our c# applications based on our requirements.