Why Partial Methods in C# ?
Partial methods in C# have been introduced to customize the type’s behavior. But how we would have done it in case if there were no partial methods. Suppose we are providing a class whose behavior we want to be customized by the user of the class. We can achieve this functionality by providing a virtual function in the class which can be overridden in the derived class as shown below.
////// Class exposed to be used by third party /// public class MyBaseClass { private string myName; public string MyName { get { return myName; } set { myName = value; OnNameChanging(); } } public virtual void OnNameChanging() { } }
Now if we want to customize the behavior of this class, we need to derive from this class and override the OnNameChanging() method in the derived class as shown below,
////// Class created by developer of third party to consume the MyBaseClass's functionaliity /// public class MyDerivedClass:MyBaseClass { public override void OnNameChanging() { Console.WriteLine("MyName is changed"); } }
Now with the above code we can notice that there can be couple of problems which can arise as discussed below:
- The class which we are exposing cannot be sealed. This way of extending the behavior of the class functionality cannot be used for the sealed classes and value types (as value types are implicitly sealed). And we cannot use this technique for the static methods since they cannot be overridden.
- In this scenario there is wastage of the system resources as even if we are not overriding the base class function’s definition in the derived class, as in the case the base class’s virtual method would be simply called and returned.
Using Partial Methods in C#
Partial methods in C# can be used to overcome all these shortcomings and still be used to extend the functionality of the methods of the class as shown in the code snippet provided below.
////////// Class exposed to be used by third party ///// internal sealed partial class MyBaseClass { private string myName; public string MyName { get { return myName; } set { myName = value; OnNameChanging(); } } partial void OnNameChanging(); } ////// Consumer class definition /// internal partial class MyBaseClass { partial void OnNameChanging() { Console.WriteLine("On name changing"); } }
Points to note in this newer version of code using partial methods.
- The class is now sealed(though it is not mandatory to be).
- The class can be a static class or even a value type.
- Both the classes have defined partial methods in each of themselves, which will be compiled as single method.
- Another important point which we should note down here is that if the consumer class does not define the partial method’s definition in that case the compiler will not emit any metadata representing the partial methods. In addition, the compiler will not emit any IL metadata representing the partial methods. The result is that there is less metadata/IL, and the runtime performance is great.
Rules for using Partial Methods in C#:
- Partial methods can only de declared within a partial value type or reference type.
- Partial methods return void and cannot accept a parameter with the out keyword. This restriction is in place as the compiler is not sure about the implementation of a partial method. Suppose in case if the methods does not exist at runtime, then in that case it would be not be possible to initialize the return type as well as the out parameter.
- The defining partial methods and the implementing partial methods should have the same signature.
- Partial methods are always considered to be private, even though they are not declared private by default
Is it possible to have multiple partial methods to have implementation?
No, it is not possible to have multiple implementation of the partial methods as it will result into an compile time error as shown below figure.
Conclusion:
In this series about the different type of the methods in C#, I have discussed about Constructors, operator overload methods, extension methods and now in this article partial methods.
With the correct knowledge about all these methods in C# we can make the programming much easier and convenient to be extensible. Please let me know your thoughts about the article.
SanMissar says
This is interesting. What would a practical use-case for partial methods be though? Partial classes are of course found in abundance but partial methods are a bit of a rarity.