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.

其他提示

It would just be confusing. Using the standard syntax, it would imply that you cannot implement the interface. Also, interfaces don't contain any functionality or fields, so there is no practical use in sealing it. An interface is more or less a contract.

Sealing an interface from "interface inheritance" would not do anything, since people could just implement your interface and the other one that would have inherited your interface.

sealed in the context of an interface would mean no class can implement this interface. That would be useless, hence its not allowed.

The sealed keyword is simply not designed (and makes no sense) for interfaces. See the msdn documentation http://msdn.microsoft.com/en-us/library/88c54tsw(v=vs.71).aspx

Sealed is a keyword for a class. The goal of interfaces is for classes to implement any contracts they define. You can seal the class that implements an interface, but sealing an interface would be of little use.

Interfaces is your Application Contract...When you don't need to your contract why you define it?

One of the primary purposes of sealing a class is to require any storage locations of that class to hold instances of that precise type, rather than a derived type. Even though most code won't mind when derived-type objects are substituted for base-type objects, there are many situations where such substitution can be problematic. For example, some classes include a function to perform a relative comparison with another object of the same type, with the expectation that the comparison results will yield a sortable sequence. If types Bar and Boz both derive from Foo, and use any Bar-specific or Boz-specific fields in the comparison, it may be impossible to sensibly sort sequences holding a mixture of Bar and Boz instances.

Such a purpose would not exist with interfaces, since by their nature interfaces are designed to be implemented by multiple classes. An interface which couldn't be implemented by any classes would be useless, and an interface which could only be implemented by one class would be pointless (if IFoo can't be anything other than a Foo, one might as well simply use Foo everyplace one would be inclined to use IFoo).

There would be some usefulness to restricting implementations of an interface to the module where it's defined, and/or allowing interfaces to have internal members. I don't know any particular reason why that isn't allowed, nor even whether allowing or disallowing such things would have been "easier" or "harder" than forbidding them.

To me, interface implies an abstract keyword; so if an interface would be sealed, it would be sealed abstract what seems to be a contradiction.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top