Learn About Partial Method In C#

Introduction

 
If you guys were following me through the last article, then you now know what partial class is. For new readers, you can visit this article on Partial class.
 
In partial class, we can have executable code in 2 or more different files right, because the entry point for the class can be anywhere, any method, event, or constructor. We never know exactly which one. After that, the compiler follows the path that we set by calling another method or constructor chain, as the developer desires.
 

Problem

 
For method, we have only one entry point. Then, the compiler executes statements one by one in a synchronized manner unless we use asynchronous programming or jump statements.
 
Let's say that I have one partial method in one file, as follows:
 
FileOne.cs
  1. partial class PlanetEarth  
  2.    {  
  3.        partial void DisplayProperties()  
  4.        {  
  5.            int num1 = 10;  
  6.            int num2 = 10;  
  7.            int result = num1 + num2;  
  8.            System.Console.WriteLine("Addition: "+result);  
  9.        }  
  10.    } 
And another partial method:
 
FileTwo.cs
  1. partial class PlanetEarth  
  2.    {  
  3.        public void DisplayProperties()  
  4.        {  
  5.            int num1 = 20;  
  6.            int num2 = 20;  
  7.            int result = num1 * num2;  
  8.            System.Console.WriteLine("Multiplication: " + result);  
  9.        }  
  10.    } 
The compiler will treat these 2 methods and create one, something like this:
  1. partial void DisplayProperties()  
  2.        {  
  3.            int num1 = 10;  
  4.            int num2 = 10;  
  5.            int result = num1 + num2;  
  6.            System.Console.WriteLine("Addition: "+result);  
  7.            int num1 = 20;  
  8.            int num2 = 20;  
  9.            int result = num1 * num2;  
  10.            System.Console.WriteLine("Multiplication: " + result);  
  11.   
  12.        } 
It will also throw a few exceptions as a gift.
 
 
The point of explaining all this is to make you realize that we simply can't chop the method's definition into two parts. It is just wrong on so many levels.
 

Partial Method

 
We can still have one method in 2 different files. Such methods are called partial methods. The condition is that they need to be declared and defined inside a partial class only.
  • Partial methods have to have a declaration in one partial class & a definition in another. We can't have a definition split into 2 partial classes, we saw that problem above.
  • It can also have both the declaration & definition in the same partial class.
  • It has no return types (i.e. only void.)
  • Both signatures should be the same.
  • Partial methods are implicitly private, the compiler won't allow for any other access to be specified.
 
DisplayProperties declaration in PlanetEarth.cs file:
  1. public partial class PlanetEarth  
  2.    {  
  3.        public string Volume { getset; }  
  4.        public string Mass { getset; }  
  5.        public string Age { getset; }  
  6.   
  7.        partial void DisplayProperties();  
  8.    } 
DisplayProperties defination in PrintEarthProperties.cs file:
  1. partial class PlanetEarth  
  2.    {  
  3.        partial void DisplayProperties()  
  4.        {  
  5.            System.Console.WriteLine("Volume: "+Volume + " Mass:"+ Mass + " Age:"+Age);  
  6.        }  
  7.    } 
Smart Compiler
  • Treats as a single class file. The compiler collects all those different files with the same class name & puts them together while compiling.
  • We saw how to chop the definition from the declaration, but it is really up to the developers and business logic whether to provide a definition or not. If the developer doesn't provide any definition, then the compiler won't even consider a declaration of the method as well.

Conclusion

 
Now we know how to use partial methods, when to use it & what are the obvious errors we can pass.
 
Partial classes & methods are powerful tools together, they keep code neat, clean & easy to understand. Believe me, it takes away a lot of confusion.
 
I suggest you use them in your projects.
 
Thank you all. Keep coding!


Similar Articles