请不变断言适合C#编程?断言、适合

由网友(侃侃而谈句句是你)分享简介:在这本书 codeRS工作,笔者问:在你如何使用你的不变量code。请解释一下这个问题的意思。In the book coders at work, the author asks "How do you use invariants in your code". Please explain what this...

在这本书 codeRS工作,笔者问:在你如何使用你的不变量code。请解释一下这个问题的意思。

In the book coders at work, the author asks "How do you use invariants in your code". Please explain what this question means.

我看到了类不变量在维基,但这个例子是在Java中,我不是不够熟练在Java中关系本例中为C#。 .NET 4.0引入不变性,协方差和逆变并很好地解释here.不变性是如此广泛。作者使用这个词似乎单元测试有关。对于那些读过书,是什么作者的意思吗?我们是在谈论做一个假设,只是测试的有效性单元测试后?

I saw class invariants on wiki, but the example is in Java and I am not skilled enough in Java to relate this example to C#. .NET 4.0 introduces invariance, covariance, and contravariance and is well explained here. Invariance is so broad. The authors usage of the word seems unit test related. For those that read the book, what does the author mean? Are we talking about making an assumption and simply testing the validity after the unit test?

推荐答案

字不变,并不意味着超过一些在一定条件下不发生变化。有许多不同种类的不变量。例如,在物理光速是不变的根据洛伦兹变换,也就是说,如果你改变引用框架不会改变。在编程有很多种不变量太多。有类不变量不超过一个对象的生命周期变化,不变的方法不函数的生命周期内发生变化,...

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,...

一个类不变的东西,总是(至少在公开的可观测时间)真正的该类的实例。

A class invariant is something that's always(at least at publicly observable times) true in an instance of that class.

这是没有办法合作/禁忌的差异有关。合作/孔特拉方差描述哪些类型可以为其他类型的地被不同的(通用)的参数或者返回类型。虽然你可以调用一些不变的,因为它不支持合作/孔特拉方差,这是一种完全不同的不变性不是一个类或方法不变。

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.

例如一些集合类可能具有以下不变量:

For example some kind of collection might have the following invariants:

数据!= NULL 尺寸> = 0 容量> = 0 在尺寸和LT =容量

使用这个类:

class MyCollection<T>
{
  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 >= 0);
    Contract.Invariant(Capacity >= 0);
    Contract.Invariant(Size < Capacity);
  }
}

几乎每个类都有一些不变量,但不是每个人强制他们。 .NET 4中增加了一个不错的方式来记录和使用code合同断言他们。

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.

阅读全文

相关推荐

最新文章