public sealed interface IMyInterface
{
}
Gives "The modified 'sealed' is not valid for this item"
I can understand in some ways that an interface must be descendable otherwise the class cannot implement it.
But why can I not specify that an interface should not have a sub interface defined or is there a way, just not with sealed
?
Edit
I should have made some effort to explain why I would want this. I often see interface inheritence chains where the dev should be using composition instead. Sealed is ideal for this in classes and I wondered if there was a way to enforce the same for interfaces. As unnessasary inheritence makes it harder to refactor and maintain in my opinion.
Edit 2
On reflection of the comments and posts, interface inheritence trees can't be anywhere near as complex as object inheritence trees. As when you are deriving from another interface IX
all you are saying is "must also implement IX
". And preventing that has no benefit.
The purpose of sealing a class, or a virtual method of a class, is to lower your costs. Designing for inheritance is expensive, and if you do not do it correctly, it is dangerous. There are security, correctness and robustness consequences to improperly designing for inheritance, so if you do not intend to design for inheritance, it is wise to seal your class and thereby avoid the costs associated with designing for inheritance.
Classes need to be designed for inheritance because they have implementation details. Interfaces have no implementation details. There is no cost associated with interfaces being inheritable. And therefore there is no incentive to add the feature of allowing interfaces to be sealed.