Tech Point Fundamentals

Thursday, September 24, 2020

Partial Interface Method in C# 8

Partial Interface Methods - C# 8

Interface-Partial-Method

  Until C# 7.x, you cannot have any partial interface member in any interface, but in C# 8, the .Net team has introduced partial interface methods for the interface as well. In this article, we will see the partial interface and partial interface methods with examples.





Introduction

Microsoft has introduced the partial types in the .Net Framework 2.0 with C# 2.0. With this new feature, you were able to split the definition of a class, interface, or structure over more than one source file.


When you define your class, interface, or struct with the partial keyword, you or someone else is allowed to extend the functionality of your class, interface, or struct with another class, interface, or struct respectively. 


The concept of a partial method was introduced by Microsoft in the .Net Framework 3.0 with C# 3.0. But partial methods were only allowed in partial class and partial structs. Partial methods were not allowed for partial interfaces. But now with the introduction of C# 8 in .Net Core 3.x, partial methods are allowed for partial interfaces as well.


Partial Interface

In C#, you can split the implementation of an interface into multiple files using the partial keyword.  The partial keyword indicates that other parts of the interface can be defined anywhere in the namespace. 


All the parts must use the partial keyword and must be available at compile time to form the final type.  The compiler combines all the implementation from multiple .cs files when the program is compiled.


Please read more about partial types here.




Fundamental Points of Partial Interface

    1. The partial modifier must be used just before the interface keyword name.
    2. All the parts must be declared with a partial keyword.
    3. Every part must be defined in the same namespace (assembly, dll, or exe).
    4. Each part must be available at compile time to form the final type.
    5. A partial interface can have partial interface methods.
    6. All the parts must have the same accessibility (public, private, protected, etc).
    7. Inheritance between the partial parts is not allowed.
    8. The interface member declared in a partial definition will be available to all the other parts.
    9. Nested partial types are allowed.
    10. At compile time XML-Comments of different parts will be merged.

Interface Partial Method

With the introduction of C# 8, partial methods are allowed in a partial interface. Until C# 7, partial methods were only allowed in either partial class or partial struct.


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. 


Please read more about partial methods here.




Interface Partial Method Example


 

Interface Partial Method Fundamental Points

    1. 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. The partial method cannot have multiple declaration definitions.
    6. Although the partial method's implementation definition is optional, you cannot have multiple implementation definitions for a partial method.
    7. A partial method can have in and ref type parameters but not out parameters. 
    8. All the partial methods are implicitly private, so cannot be virtual.
    9. A partial method cannot be extern, because the presence of the body determines whether they are defining or implementing.
    10. A partial method cannot be abstract or sealed.
    11. A partial method can be static or unsafe.
    12. 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.
    13. You can also have partial generic methods.
    14. Method overloading is allowed for partial methods.



Live Demo




Conclusion

In C# 8, partial methods are allowed in the partial interface as well. The interface partial method has the same rules as it was for other partial types like partial class and partial structs.




Recommended Articles


Thanks for visiting this page. Please follow us on Twitter, Facebook, LinkedIn, Youtube, 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.