C# 8 Interface Member Modifiers | abstract, virtual, sealed, static, partial, extern
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
- Can an interface contain abstract methods in C# 8?
- A private abstract interface method is allowed or not in C# 8?
- Can you make a virtual interface method as abstract in C# 8?
- Is the sealed interface method valid in C# 8?
- Can you have a sealed interface method without body definition in C# 8?
- Can you override a sealed interface method in C# 8?
- Can a private interface member be sealed in C# 8?
- Can an interface contain static interface members in C# 8?
- Is it possible to have a static interface method without body definition in C# 8?
- An abstract interface method can be static or not in C# 8?
- 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.
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
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.
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.
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.
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.
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
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 Quora for regular updates.
Video Recommendation
No comments:
Post a Comment
Please do not enter any HTML. JavaScript or spam link in the comment box.