Writing code contracts on methods of increasing activity

I write code contracts on an interface that uses events:

interface EventInterface<T> {
    event EventHandler ItemAdded;
    bool Add(T item);
}

When an item is added to a collection that implements the interface, the collection should raise an event ItemAdded. An event should only be raised if an item has been added; this is set by the return value (think of a set, where it truemeans that the element was added, falsemeans that it was not added, because it already exists in the set).

I want to have a contract that ensures that if the result is correct, the event will be raised. Similarly, if the result is false, the event is not raised. Is there any way to verify that using contracts?

+4
source share
1 answer

This is not for code contracts . With an interface, you can specify only information about the parameters and the return value of the interface methods.

You might want to write an abstract base class that contains event logic, or you can tweak for this.

+1
source

Source: https://habr.com/ru/post/1622732/


All Articles