Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The word invariant doesn't mean more than something doesn't change under certain conditions. There are many different kinds of invariants. For example in physics the speed of light is invariant under lorentz-transform, i.e. it doesn't change if you change to reference frame. In programming there are many kinds of invariants too. There are class invariants which don't change over the lifetime of an object, method invariants which don't change during the life of a function,...</p> <p>A class invariant is something that's always(at least at publicly observable times) true in an instance of that class.</p> <p>This is in no way related to co-/contra-variance. Co-/Contra-variance describes which types can be substituted for other types with different (generic) parameters or return types. While you can call something invariant because it doesn't support Co-/Contra-variance this is a completely different kind of invariance than a class or method invariant.</p> <p>For example some kind of collection might have the following invariants:</p> <ul> <li>data != null</li> <li>Size >= 0</li> <li>Capacity >= 0</li> <li>Size &lt;= Capacity</li> </ul> <p>With this class:</p> <pre><code>class MyCollection&lt;T&gt; { private T[] data; private int size; public MyCollection() { data=new T[4]; } public int Size{get{return size;}} public int Capacity{get{return data.Length;}} [ContractInvariantMethod] protected void ClassInvariant() { Contract.Invariant(data != null); Contract.Invariant(Size &gt;= 0); Contract.Invariant(Capacity &gt;= 0); Contract.Invariant(Size &lt; Capacity); } } </code></pre> <p>Almost every class has some invariants, but not everybody enforces them. .net 4 adds a nice way to document and assert them using code contracts.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload