CSharp8


Interface 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.

  1. Can an interface contain abstract methods Yes, you can have an abstract keyword explicitly while declaring the interface member in C# 8.
  2. A private abstract interface method is allowed or notNo, 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.
  3. Can you make a virtual interface method as abstract No, both the keywords are contradictory. Abstract members have nobody definition while a virtual member must have a body definition.
  4. Is sealed interface method valid Yes, you can have sealed interface members in C# 8.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.
  5. Can you have a sealed interface method without body definition – No, each sealed member must have a body definition.
  6. Can you override a sealed interface method 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.
  7. Can a private interface member be sealed -No, a sealed member cannot be private.
  8. Can an interface contain static interface members
  9. Is it possible to have a static interface method without body definition
  10. An abstract interface method can be static or not
  11. Extern interface methods are allowed or not