Add contract to interface implementation

大憨熊 提交于 2019-12-01 09:25:40

You can't - that postcondition isn't appropriate for all implementations of IFoo, because it's not declared in IFoo. You can only refer to members of the interface (or other interfaces it extends).

You should be able to add it in Foo though, because you're adding a postcondition (Ensures) rather than a precondition (Requires).

You can't add an implementation-specific precondition because then a caller wouldn't be able to know whether or not they were going to violate the contract:

public void DoSomething(IFoo foo)
{
    // Is this valid or not? I have no way of telling.
    foo.Do(bar);
}

Basically, contracts aren't allowed to be "unfair" to callers - if the caller violates a precondition, that should always indicate a bug rather than something they couldn't have predicted.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!