In c#, the 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 the same partial class.
Generally, the partial methods will exist 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 represent 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, then the compiler will remove the definition of the method 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.
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().
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 you execute the above code, the compiler will combine these two partial classes into one User class like as shown below.
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.
In c#, we need to follow certain rules to implement the partial method in our applications.
partial
keyword and method must return void.Following is the example of defining a partial method in 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 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 as shown below.
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.