Invariants

Invariants are special contracts that ensure an object is considered valid during all its lifetime. Invariant contracts are provided inside one method decorated with the ContractInvariantMethod attribute that affects all members in the enclosing class. Only one invariant method can be declared inside a class, and typically it should be marked as Protected to avoid risk of calls from clients. The method is by convention named ObjectInvariant (although not mandatory) and is used instead of preconditions and postconditions. The following code snippet provides an example:

image

Simply this code establishes that during the entire lifetime of the ...

Get Visual Basic® 2010 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.