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 problems, top 100 SQL problems, resources, and mock tests to enhance your knowledge.
Do upvote our blog to help other ninjas grow. Happy Coding!