Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

Partial Method in C#

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction 

In C#, a partial method is a special method with a declaration component in one partial class and a definition portion in another partial class or the same partial class.

Partial methods are usually found in partial classes or structs, and they are divided into two parts: declaration and definition. The declaration part represents the partial method's signature, while the definition part represents the partial method's implementation. As shown below, the partial keyword is used to declare a partial method.

Syntax 

partial void MethodName
{
    // Code
}

Rules to Implement Partial Method

To implement the partial method in our applications in C#, we must follow certain rules.

  • The signature of the partial method must be the same in both partial classes.
  • The partial method declaration must start with the keyword partial and return void.
  • Partial methods are implicitly private by default. Therefore they can't have access modifiers and can't be virtual.
  • Partial methods are implicitly private.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Implementation of Partial Method

Here is an example of implementing a partial method using a partial keyword in two class files, Square1.cs and Square2.cs.

Square1.cs

public partial class Square
{
    // Partial Method Declaration
    partial void area(int s);
    public void print()
    {
        Console.WriteLine("Test");
    }
}

In the above code, we declared a partial method called area in the Square1.cs class file using a partial keyword along with a normal method called print().

Square2.cs

public partial class Square
{
    public void new_area(int s)
    {
        area(int s);
    }
    // Partial Method Implementation
    partial void area(int a)
    {
        int val=a*a;
        Console.WriteLine("Area of the square is : {0}", val);
    }
}

The above code contains the implementation of the area method.

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

Square

public class Square
{
    public void print()
    {
        Console.WriteLine("Test");
    }
    public void new_area(int s)
    {
        area(int s);
    }
    private void area(int a)
    {
        int val=a*a;
        Console.WriteLine("Area of the square is : {0}", val);
    }
}

 

Must Read IEnumerable vs IQueryable.

C# Partial Method Example

Here is an example of defining a partial method in a partial class using a partial modifier in the C# programming language.

Example:

using System;  
namespace Coding 
{  
    partial class A  
    {  
        // Partial Method Declaration
        partial void Display(string MSG);  
    }  
    partial class A  
    {  
        // Partial Method Implementation 
        partial void Display(String MSG)  
        {  
            Console.WriteLine(MSG);  
        }  
        public static void Main(string[] args)  
        {  
            // Calling partial method.  
            new A().Display("Welcome to the Coding Ninjas.");  
        }  
    }  
} 

Output

Welcome to the Coding Ninjas.

Related Article: Singleton Design Pattern C#

FAQs

What is the use of partial methods in C#?

Similar to events, partial methods enable the implementer of one part of a class to define a method. The other part of the class's implementer might choose whether or not to implement the method. The compiler eliminates the method signature and all calls to the method if the method is not implemented.

What is the use of partial class in C#?

A partial class is a class that has its definition in two or more files. Each source file includes a class section, and when the application is compiled, all parts are combined.

Can partial classes be in different namespaces?

Partial classes can only exist in the same namespace and assembly. Partial classes cannot be in two different assemblies.

Can we have a constructor in partial class C#?

You can have multiple constructors in a class defined with partial, but each constructor must be unique (even if they're in different files).

Conclusion 

In this article, we have extensively discussed the Partial method in C# progarmming language.

We hope that this article has helped you enhance your knowledge regarding the Partial method in C# and if you would like to learn more, check out our article on the Differences Between C++ and C# and C# interview questions for 5 years Experience.

Refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.

Refer to the links problemstop 100 SQL problemsresources, and mock tests to enhance your knowledge.

Do upvote our blog to help other ninjas grow. Happy Coding!

Topics covered
1.
Introduction 
2.
Rules to Implement Partial Method
3.
Implementation of Partial Method
4.
C# Partial Method Example
5.
FAQs
5.1.
What is the use of partial methods in C#?
5.2.
What is the use of partial class in C#?
5.3.
Can partial classes be in different namespaces?
5.4.
Can we have a constructor in partial class C#?
6.
Conclusion