Tech Point Fundamentals

Tuesday, September 22, 2020

Partial Methods with Live Example in C#

Partial Methods with Live Example in C#

Partial-Methods-In-CSharp

  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.


Watch our videos 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


    1. In which version of the C# partial method was introduced?
    2. Can you declare a partial method in non-partial types?
    3. Which return types can you specify for a partial method?
    4. Can you specify access modifiers explicitly for partial methods?
    5. Is it mandatory to implement the partial methods?
    6. Can you implement the partial method along with the declaration?
    7. Can a partial method contain ref type parameters?
    8. Can a partial method define out type parameters, why?
    9. Can a partial method be extern, why?
    10. Can you define virtual partial methods, why?
    11. Can you create a delegate to the partial method?
    12. Can a partial method be marked as abstract or sealed?
    13. Can a partial method be static or unsafe?
    14. What is the difference between partial method, abstract method, virtual method, and abstract interface method?
    15. Can you override the base class abstract method as a partial method?
    16. Can you define multiple implementations for a declared partial method?
    17. Can you override a partial method in the inheriting class?
    18. Can you overload a partial method?
    19. What is the default access level of a partial method?
    20. Can you define a partial method in a partial interface?




Partial Method


A method defined with the keyword partial is known as a partial method in C#. A partial method can be split into two separate code files of partial types.  

A partial method declaration consists of two parts: the definition and the implementation. Usually, a partial method has its signature defined in one part of a partial type, and its implementation may be defined in another part of the partial type.  But it is valid to have both in the same part of the partial type. 

One of the partial type files (.cs file) must contain a signature of the method, and other files can contain the optional implementation of the partial method. But both declaration and implementation of a method must have the partial keyword.

It is mandatory to have the signature of the partial method, but it is not mandatory to provide the implementation. A partial method gets executed only when it has an implementation. If the method is not implemented, then the compiler removes the method signature and all calls to the method.

There will be no compile-time or run-time errors if the partial method is called but not implemented. 
If the implementation is not supplied, then the method and all calls to the method are removed at compile time by the compiler itself.

Until C# 7, partial methods are not allowed in the partial interface but in C# 8, you can define partial methods in the partial interface as well.



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().

Partial-Method-Assembly-Code-View
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


    1. A partial method can only be created in partial types (partial class, partial struct, or partial interface).
    2. The return type of a partial method must be void.
    3. You cannot specify any access modifiers explicitly even private to a partial method, they are private implicitly by default.
    4. Both declaration and implementation of a partial method must use the partial keyword.
    5. Method signatures in both parts of the partial type must match.
    6. 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.
    7. The partial method cannot have multiple declaration definitions.
    8. Although the partial method's implementation definition is optional, you cannot have multiple implementation definitions for a partial method.
    9. 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.  
    10. All the partial methods are implicitly private, so cannot be virtual.
    11. A partial method cannot be extern, because the presence of the body determines whether they are defining or implementing.
    12. A partial method cannot be abstract or sealed.
    13. A partial method can be static or unsafe.
    14. 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.
    15. You can also have partial generic methods.
    16. Method overloading is allowed for partial methods.
    17. You can also declare and define partial events as well.
    18. 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


    1. The partial method was introduced in C# 3.0 with the .Net Framework 3.0.
    2. Partial methods can only be defined in partial types (partial class, partial struct, and partial interface).
    3. The return type of a partial class must be void.
    4. You cannot specify any access modifiers to a partial class all are private by default.
    5. Implementation of a partial method is optional but the declaration is mandatory.
    6. You can have ref parameters, but not out parameters.
    7. A partial method cannot be abstract, sealed, extern, or virtual.
    8. You can make delegate to a partial method only if it is implemented.
    9. You can have static and unsafe partial methods.
    10. You cannot override the base class abstract method as partial.



Recommended Articles


Thanks for visiting this page. Please follow us on Twitter, Facebook, LinkedIn, Youtube, Quora, and Pinterest for regular updates.




Video Recommendation

Watch More Videos...

No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.