Am I implementing this simple contract incorrectly?

后端 未结 1 998
时光取名叫无心
时光取名叫无心 2021-02-07 07:27

This is my code:

public class RegularPolygon
{
    public int VertexCount;
    public double SideLength;

    public RegularPolygon(int vertexCount, double sideL         


        
1条回答
  •  失恋的感觉
    2021-02-07 08:26

    First thing to check - have you actually got contract checking turned on? If not, none of your contracts will do anything. That would explain the R# warning, too. Look under "Code Contracts" in the build properties, and see what it says under "Runtime Checking".

    As per comments, ensure you have CONTRACTS_FULL defined as a compilation symbol - that appears to be what R# requires.

    Second point: you've got public mutable fields, which means your invariant can be violated at any moment by someone writing

    polygon.VertexCount = 0;
    

    Please don't use public fields, particularly not writable ones. :)

    0 讨论(0)
提交回复
热议问题