Debug.Assert的VS代码使用合同 [英] Debug.Assert vs Code Contract usage

查看:115
本文介绍了Debug.Assert的VS代码使用合同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Debug.Assert的代码合同或反之亦然?我想检查先决条件的方法,我很困惑,选择一个比其他。我有单元测试,我想测试的故障情况,并期望例外。

When should I debug.assert over code contracts or vice versa? I want to check precondition for a method and I am confused to choose one over the other. I have unit tests where I want to test failure scenarios and expect exceptions.

这是一个好的做法是使用Debug.Assert的代码和合约上的同样的方法。 ?如果是这样,这将是其中的代码应该写入的顺序

Is it a good practice to use Debug.Assert and Code contract on the same method. If so what would be the order in which the code should be written?

Debug.Assert(parameter!= null);
Contract.Requires<ArgumentNullException>(parameter != null, "parameter");

Contract.Requires<ArgumentNullException>(parameter != null, "parameter");
Debug.Assert(parameter!= null);

有它背后的原因是什么?

Is there any rationale behind it?

推荐答案

这是不同的东西。当代码编译为调试,因此将只检查/下调试断言时,才执行调试断言。我们的想法是使用它进行健康检查为代码,您正在开发。合约代码可在调试或发布使用。它们保证的方法前后条件符合的方法的期望(符合合同)。还有一个测试框架,它提供了类似的功能,专为检查一致性测试。

These are different things. A debug assert is only executed when the code is compiled as debug and therefore will only check/assert under debug. The idea is to use this for "sanity checks" for code you are developing. Code contracts can be used in either debug or release. They assure that pre and post conditions of methods comply with the expectations of the method (meet the contract). There is also a testing framework that provides similar functionality, designed for checking test compliance.

使用Debug.Assert的,当你想确保某些事情是你期望开发的代码(并在以后的维护开发)时。

Use Debug.Assert when you want ensure that certain things are as you expect when developing the code (and in later maintenance development).

当你要保证条件在这两个调试和发布真正使用代码合同。合同还允许静态分析的某些形式可以在验证你的程序是正确是有帮助的。

Use code contracts when you want to assure that conditions are true in both debug and release. Contracts also allow certain forms of static analysis that can be helpful in verifying that your program is "correct".

创建单元测试时使用的测试框架断言。

Use the Testing framework assertions when creating unit tests.

这篇关于Debug.Assert的VS代码使用合同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆