C# Partial Method

  Keywords : c# partial method or function, c# partial method implementation, c# partial method return value, c# partial class method example, partial methods in c#

Here we will learn partial method in c# with examples and how to use c# partial method to split the functionality of single method with examples

C# Partial Method

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

 

Generally, the partial methods will exists either in partial class or struct and it 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 will represents an implementation of partial method.

 

In partial method, the definition part is an optional so it may or may not have an implementation in partial classes. In case, the implementation part of partial method is not available in any partial class, then the compiler will remove the definition of method and all the calls related to that particular method in 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 above code, we declared a partial method called GetUserDetails in User1.cs class file using partial keyword along with normal 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 above code, we implemented a partial method called GetUserDetails in User1.cs class file using partial keyword with required variables and constructor.

 

When we execute above code, the compiler will combine these two partial classes into one User class like 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 single class while executing the application in c# programming langauge.

Rules to Implement Partial Method

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

 

  • In c#, the partial method can be implemented within partial class or structure only and the signatures in both parts of partial type must be same.
  • The partial method declaration must begin with partial keyword and method must return void.
  • By default partial methods are implicitly private so no access modifiers are allowed 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 partial class using partial modifer 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 Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

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

 

When we execute above c# program, we will get the result like as shown 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. 

 

PREVIOUS

C# Partial Class