Partial Interface Methods - C# 8
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
- The partial modifier must be used just before the interface keyword name.
- All the parts must be declared with a partial keyword.
- Every part must be defined in the same namespace (assembly, dll, or exe).
- Each part must be available at compile time to form the final type.
- A partial interface can have partial interface methods.
- All the parts must have the same accessibility (public, private, protected, etc).
- Inheritance between the partial parts is not allowed.
- The interface member declared in a partial definition will be available to all the other parts.
- Nested partial types are allowed.
- 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.
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.
Please read more about partial methods here.
Interface Partial Method Example
Interface 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.
- 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.
- 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.
Live Demo
Conclusion
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, 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.