Here we will learn what is a partial method in c#, uses of partial methods, and partial method implementation in c# with examples.
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#.
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#.
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.cs, File2.cs) into a single User class as shown below.
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.
The following is an example of defining the partial methods with int
return type in c#.
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.
Following is the example of defining the partial methods with the same signature in c#.
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.
Following is the example of implementing the partial methods in a partial class using partial
keyword in c#.
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.
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.
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.
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.
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#.
While working with partial methods in c#, remember the following rules.
partial
keyword.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.This is how we can create and use partial methods in our c# applications based on our requirements.