Tech Point Fundamentals

Sunday, September 27, 2020

Interface Modifiers C# 8

C# 8 Interface Member Modifiers | abstract, virtual, sealed, static, partial, extern

Interface-Member-Modifiers

Before C# 8 interface has no modifiers at all to any of the interface members like methods, properties, events, and indexers. But from C# 8 it is now valid to add modifiers like virtual, abstract, sealed, static, extern, partial for the interface members. In this article, we will see all the modifiers with a live example.




Introduction

Microsoft has introduced C# 8 with .Net Core 3.x in Visual studio 2019. In C# 8 Microsoft has introduced a lot of new features for the interface as well. C# 8 is only available in .Net Standard 2.1 and .Net Core 3.x implements the same.


You can specify explicit access modifiers for interface members. An interface member can also have a default body implementation.  An interface member can have special modifiers like abstract, virtual, sealed, static, partial, and extern as well.






Today's Interview Questions


    1. Can an interface contain abstract methods in C# 8?
    2. A private abstract interface method is allowed or not in C# 8?
    3. Can you make a virtual interface method as abstract in C# 8?
    4. Is the sealed interface method valid in C# 8?
    5. Can you have a sealed interface method without body definition in C# 8?
    6. Can you override a sealed interface method in C# 8?
    7. Can a private interface member be sealed in C# 8?
    8. Can an interface contain static interface members in C# 8?
    9. Is it possible to have a static interface method without body definition in C# 8?
    10. An abstract interface method can be static or not in C# 8?
    11. Extern interface methods are allowed or not in C# 8?




Abstract Interface Members in C# 8


You can have abstract interface members in C# 8. All the public interface members are abstract by default which has nobody. You can have an abstract keyword explicitly also in C# 8.


Please read more about the abstract methods here.


By using the abstract keyword doesn’t make any difference to the inheriting classes as the inheriting classes need to implement all the public abstract interface methods (whether or not marked as abstract).



Can an interface contain abstract methods in C# 8?

Yes, you can have an abstract keyword explicitly while declaring the interface member in C# 8.


A private abstract interface method is allowed or not in C# 8?

No, an abstract member cannot be private in C# 8. It will generate a compile-time error. An abstract member must be implemented by the inheriting class and by making it private, it will be no longer accessible to the inheriting class or interface.


Can you make a virtual interface method as abstract in C# 8?

No, both the keywords are contradictory. Abstract members have nobody definition while a virtual member must have a body definition.




Example


 


Virtual Interface Members in C# 8


An interface can also contain virtual members in C# 8. Whenever you are making any member as virtual, you must have to provide the default body definition for the same. Also, you cannot use static, abstract, private, or override modifiers for any virtual member.  


Please read more about the default interface methods here.

The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class or interface


Please read more about the virtual methods here.

So the implementation of a virtual member can be changed by an overriding member in the derived class or interface. You can read more about multiple interface inheritance and overriding rules in interface here.


Please read more about the virtual interface members here.



Sealed Interface Members in C# 8


In C# 8 you can also have sealed members in the interface. You can declare a non-virtual member by using a sealed keyword.


But remember when you are making any member as sealed then you must have to provide the body definition as well, otherwise, it will cause a compile-time error.


Please read more about the sealed methods here.


Is the sealed interface method valid in C# 8?

Yes, you can have sealed interface members in C# 8.


Can you have a sealed interface method without body definition in C# 8?

No, each sealed member must have a body definition.


Can a private interface member be sealed in C# 8?

No, a sealed member cannot be private.


Can you override a sealed interface method in C# 8?

No, a sealed member cannot be overridden in C# 8. Also, an overridden virtual member can't be marked as sealed in the inheriting interface.




Static Interface Members in C# 8


In C# 8 you can have static interface members as well. You can have static properties that can be used for the parameterization for default implementation.

But still, you can't have any instance members in the interface. If you are making any member as static then you have to provide the definition for the same. You can also mark a private member as static.


Please read more about the static methods here.


Can an interface contain static interface members in C# 8?

Yes, you can have static members in C# 8.


Is it possible to have a static interface method without body definition in C# 8?

No, you cannot define a static member without body definition in C# 8.


An abstract interface method can be static or not in C# 8?

No, you cannot make an abstract interface member as static in C# 8.


Can you have an instance field member in C# 8?

No, you cannot have any instance field members in C# 8.


Can you define constructor or destructor in the interface in C# 8?

No, constructor or destructor is not allowed in the interface.




Partial Interface Methods in C# 8


Microsoft has introduced the partial types in the .Net Framework 2.0 with C# 2.0. 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. 


Please read more about the partial types here.

The concept of the 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.


Please read more about the partial interface methods here.

 A partial method can be split into two separate code files of partial types.  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. 
You must have to follow all the rules of partial methods.


Please read more about the partial methods here.




Explicit Interface Access Modifiers in C# 8


Until C# 7.X explicit access modifiers were not allowed for any interface member even the public. All interface members were public and abstract by default. The C# syntax for an interface is extended to accept more keywords like public, private,  protected, internal, protected internal.


There are six access modifiers in C# called public, private, protected, internal, protected internal, and private protected. The last one i.e. private protected is introduced in C# version 7.2 and it is only valid in C# version 7.2 and later.


Please read more about the explicit interface access modifiers here.




Interface Default Implementation in C# 8


Until C# 7.3 we don’t have any ability to define the method definition, but in C# 8 we have the ability to have default method implementation.


Default interface methods are also known as Virtual Extension Methods, which will allow C# developers to use the Traits Programming Technique. Traits are object-oriented programming technology that promotes the reuse of methods between unrelated classes. We can now have default method definitions in the interface.



Please read more about the default interface method here.



Extern Interface Members in C# 8


You can also have extern members in the C# 8 interface. The extern modifier is used to declare a method that is implemented externally. 


It is an error to use the abstract and extern modifiers together to modify the same member. Using the extern modifier means that the method is implemented outside the C# code, whereas using the abstract modifier means that the method implementation is not provided in the class.


A common use of the extern modifier is with the DllImport attribute when you are using Interop services to call into unmanaged code.


The extern keyword can also define an external assembly alias, which makes it possible to reference different versions of the same component from within a single assembly




Live Example





Conclusion

Microsoft has introduced a lot of new features for the interface in C# 8 which were not there in the traditional old versions of C#. In C# 8 you can have abstract methods, virtual methods, static methods, partial methods, and extern methods. You are also allowed to specify the access modifiers for the interface members explicitly.


Recommended Articles



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