Partial Methods with Live Example in C#
We know that we can declare a function signature only either in an abstract class as an abstract method or in an interface; otherwise, you must have to provide the definition to the method where you are declaring the function except extern methods. But you can declare the method signature without body also by partial keyword. In this article, we will see the partial methods in detail with examples.
Please read the previous part of Partial Type and Partial Class here.
Introduction
We already know the concept of partial class where we can split the class definition into multiple source files in an earlier version of the .Net framework. With the introduction of C# 3.0 in ".NET Framework 3.0", the partial types get a new member type i.e. partial method, which allows the developers to separate method definition and implementation. Please click here to read more about partial types and partial classes.
Today's Interview Questions
- In which version of the C# partial method was introduced?
- Can you declare a partial method in non-partial types?
- Which return types can you specify for a partial method?
- Can you specify access modifiers explicitly for partial methods?
- Is it mandatory to implement the partial methods?
- Can you implement the partial method along with the declaration?
- Can a partial method contain ref type parameters?
- Can a partial method define out type parameters, why?
- Can a partial method be extern, why?
- Can you define virtual partial methods, why?
- Can you create a delegate to the partial method?
- Can a partial method be marked as abstract or sealed?
- Can a partial method be static or unsafe?
- What is the difference between partial method, abstract method, virtual method, and abstract interface method?
- Can you override the base class abstract method as a partial method?
- Can you define multiple implementations for a declared partial method?
- Can you override a partial method in the inheriting class?
- Can you overload a partial method?
- What is the default access level of a partial method?
- Can you define a partial method in a partial interface?
Partial Method
Example of Partial Methods
What will happen if a partial method is defined and called but never implemented?
In the above example, you can see that the partial method GetUserPhoneNumber() is defined and called but not implemented anywhere. In this case, the compiler will remove both the partial method declaration definition and its call if any.
To prove the above statement, if you check the generated assembly code in "ILDASM" you can see that there is no such method at all for GetUserPhoneNumber().
Assembly code view of a partial method |
In the above image you can see there is no method with the name GetUserPhoneNumber(), and also in GetUserDetails() method GetUserEmail() is the last method which is called. GetUserPhoneNumber() partial method call is removed by the compiler at compile time.
Partial Method Fundamental Points
- A partial method can only be created in partial types (partial class, partial struct, or partial interface).
- The return type of a partial method must be void.
- You cannot specify any access modifiers explicitly even private to a partial method, they are private implicitly by default.
- Both declaration and implementation of a partial method must use the partial keyword.
- Method signatures in both parts of the partial type must match.
- You cannot provide the definition along with the implementation for a partial method. Both definition and implementation of a partial method must be in separate partial type files or in the same partial type but separately.
- The partial method cannot have multiple declaration definitions.
- Although the partial method's implementation definition is optional, you cannot have multiple implementation definitions for a partial method.
- A partial method can have in and ref type parameters but not out parameters. Out parameters are like return values which must be assigned by the callee, and we know that a partial method has optional implantation in the case if it does not have any implementation how will it assign value to the out parameters.
- All the partial methods are implicitly private, so cannot be virtual.
- A partial method cannot be extern, because the presence of the body determines whether they are defining or implementing.
- A partial method cannot be abstract or sealed.
- A partial method can be static or unsafe.
- You can create a delegate to a partial method that has been declared and implemented, but not to a partial method that has only been declared.
- You can also have partial generic methods.
- Method overloading is allowed for partial methods.
- You can also declare and define partial events as well.
- You cannot override the base class abstract method as partial.
Why Partial Methods
Partial methods are used to customize auto-generated codes. Partial methods allow for a method name and signature to be reserved, so that generated code can call the method but the developer can decide whether to implement the method or not.
The Partial methods allow the codes created by a code generator and code created by a human developer to work together without any run-time costs.
A partial method declaration behaves as a hook that will bind to the partial method implementation if it is provided by the developer.
Partial Method vs Abstract Method vs Abstract Interface Method vs Virtual Method
A partial method can only be declared in partial types (partial class, partial struct, partial interface) and the implementation is optional, if not implemented compiler remove them at compile time.
An abstract method can only be defined in an abstract class, and the inheriting class must have to override the abstract method. Read more about the difference between abstract class and interface here.
A virtual method can be defined in any class but it must have a default body implementation. The inheriting class may or may not override the virtual method.
An abstract interface method can only be defined in an interface, and it must be implemented in the inheriting class.
Live Demo
Conclusion
- The partial method was introduced in C# 3.0 with the .Net Framework 3.0.
- Partial methods can only be defined in partial types (partial class, partial struct, and partial interface).
- The return type of a partial class must be void.
- You cannot specify any access modifiers to a partial class all are private by default.
- Implementation of a partial method is optional but the declaration is mandatory.
- You can have ref parameters, but not out parameters.
- A partial method cannot be abstract, sealed, extern, or virtual.
- You can make delegate to a partial method only if it is implemented.
- You can have static and unsafe partial methods.
- You cannot override the base class abstract method as partial.
Recommended Articles
Multiple Interface Inheritance in C# 8
Interface Virtual Method in C# 8
Interface Access Modifiers in C# 8
Interface Default Methods in C# 8
Thanks for visiting this page. Please follow us on Twitter, Facebook, LinkedIn, Youtube, Quora, and Pinterest for regular updates.
Video Recommendation
No comments:
Post a Comment
Please do not enter any HTML. JavaScript or spam link in the comment box.