C# 断言 Assert
重构-断言
- Boolean b1 = false;
- System.Diagnostics.Debug.Assert(b1);
断言(Assert)与异常(Exception)
断言是被用来检查非法情况而不是错误情况,即在该程序正常工作时绝不应该发生的非法情况,用来帮助开发人员对问题的快速定位。异常处理用于对程序发生异常情况的处理,增强程序的健壮性、容错性,减少程序使用中对用户不有好的行为,不让(通常也不必)用户知道发生了什么错误。
实际开发中,我们通常将Assert与异常混淆, 不知道什么时候使用Assert,什么时候使用异常处理。或者不用Assert,将一切情况都归为异常。这样一来,就掩盖了问题,当问题发生的时候,很难进行定位,而这些问题本该是在开发的时候就解决掉的。同时,也增加了开销(在c#中,debug.Assert()编译成release版本时,不会产生任何代码,而try/catch在debug/release版本中都是有代码产生,运行时需要开销)。
Assert类位于Microsoft.VisualStudio.TestTools.UnitTesting 命名空间下,该命名空间提供支持单元测试的类。此命名空间包含许多属性,这些属性为测试引擎标识有关数据源、方法执行顺序、程序管理、代理/主机信息以及部署数据的测试信息。Microsoft.VisualStudio.TestTools.UnitTesting 命名空间还包含自定义单元测试异常。
MSDN中,有详细的Assert类的公共方法:http://msdn.microsoft.com/zh-cn/library/Microsoft.VisualStudio.TestTools.UnitTesting.Assert(v=vs.100).aspx。
本文简单归类一些简单的使用方法。掌握Assert类,一个BestPractice就是,了解Assert的几个主要方法,然后熟悉其重写方法即可,整理表格如下:
|
Name |
Description |
|
AreEqual(+18) AreNotEqual(+18) |
Verifies that specified values are equal(or not equal). The assertion fails if the values are not equal(or equal). Displays a message if the assertion fails, and applies the specified formatting to it.(if available) |
|
AreSame(+3) |
Verifies that specified object variables refer to the same object. The assertion fails if they refer to different objects. Displays a message if the assertion fails, and applies the specified formatting to it.(if available) |
|
Equal |
Determines whether two objects are equal. |
|
Fail(+3) |
Fails an assertion without checking any conditions. Displays a message, and applies the specified formatting to it.(if available) |
|
InConclusive(+3) |
Indicates that an assertion cannot be proven true or false. Also used to indicate an assertion that has not yet been implemented. Displays a message, and applies the specified formatting to it.(if available) |
|
IsFalse(+3) IsTrue(+3) |
Verifies that a specified condition is false(or true). The assertion fails if the condition is true(not true). Displays a message, and applies the specified formatting to it.(if available) |
|
IsInstanceOfType(+3) IsNotInstanceOfType(+3) |
Verifies that a specified object is(or is not) an instance of the specified type. The assertion fails if the type is not(or is) found in the inheritance hierarchy of the object. Displays a message, and applies the specified formatting to it.(if available) |
|
IsNull(+3) IsNotNull(+3) |
Verifies that a specified object is null(or not null) . The assertion fails if it is not null(or is null). Displays a message if the assertion fails, and applies the specified formatting to it.(if available) |
|
ReplaceNullChars |
In a string, replaces null characters ('\0') with "\\0". |
关于异常处理, 在编写代码的时候,应充分考虑各种具体异常,而不简单的catch到Exception,写出更健壮的代码。
通常来说,能够用Assert的地方,都可以用try/catch处理 。但这不是好习惯。We need to "Writing Clean Code".
文中相关扩展连接,
C# 断言 Assert的更多相关文章
- 断言(assert)的用法
我一直以为assert仅仅是个报错函数,事实上,它居然是个宏,并且作用并非“报错”. 在经过对其进行一定了解之后,对其作用及用法有了一定的了解,assert()的用法像是一种“契约式编程”,在我的理解 ...
- C语言中断言ASSERT
我一直以为assert仅仅是个报错函数,事实上,它居然是个宏,并且作用并非"报错". 在经过对其进行一定了解之后,对其作用及用法有了一定的了解,assert()的用法像是一种&qu ...
- 使用断言assert
之前有看过关于Assert的书,但是不懂得如何去用,最近看别人写的代码有用这个断言(assert),今天自己动手看看如何使用断言. 断言(assert)的语义如下:如果表达式的值为0(假),则输出错误 ...
- 断言Assert的使用
转载地址:http://www.cnblogs.com/moondark/archive/2012/03/12/2392315.html 我一直以为assert仅仅是个报错函数,事实上,它居然是个宏 ...
- K:java 断言 assert 初步使用:断言开启、断言使用
@转自天地悠悠的个人博客 主要总结一下在eclipse中如何使用断言. (一)首先明确: java断言Assert是jdk1.4引入的. jvm 断言默认是关闭的. 断言是可以局部开启的,如:父类禁止 ...
- java的断言(assert)
概述 在C和C++语言中都有assert关键,表示断言.在Java中,同样也有assert关键字,表示断言,用法和含义都差不多.在Java中,assert关键字是从JAVA SE 1.4 引入的,为了 ...
- JavaScript之调试工具之断言assert
1.单点断言 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...
- 断言(assert)
断言(assert):用来调试.测试代码. 格式: assert 布尔表达式: 字符串 (如果布尔表达式为false时,这个字符串才会显示). 注意: assert默认是关闭的,使用时需要使用&quo ...
- java断言assert初步使用:断言开启、断言使用
1 说明 java断言assert是jdk1.4引入的. jvm断言默认是关闭的. 断言可以局部开启的,如:父类禁止断言,而子类开启断言,所以一般说“断言不具有继承性”. 断言只适用复杂的调式过程. ...
随机推荐
- 深度学习识别CIFAR10:pytorch训练LeNet、AlexNet、VGG19实现及比较(二)
版权声明:本文为博主原创文章,欢迎转载,并请注明出处.联系方式:460356155@qq.com AlexNet在2012年ImageNet图像分类任务竞赛中获得冠军.网络结构如下图所示: 对CIFA ...
- Appium could not connect to server are you sure it's running appium desktop
use remote host value : 127.0.0.1 switch to Custom server to Automatic server adb kill-server adb st ...
- JS操作iframe元素
1. demo1.html页面中有个iframe元素,iframe元素的src是iframe1.html,怎么在demo1.html页面中操作iframe1.html页面 答曰:demo1.html ...
- AtCoder ABC 042D いろはちゃんとマス目 / Iroha and a Grid
题目链接:https://abc042.contest.atcoder.jp/tasks/arc058_b 题目大意: 给定一个 H * W 的矩阵,其中左下角 A * B 区域是禁区,要求在不踏入禁 ...
- html中滚动小球的demo
类似于下图的效果: 代码: <!DOCTYPE html> <html> <head> <title>Promise animation</tit ...
- Zookeeper+Kafka集群部署(转)
Zookeeper+Kafka集群部署 主机规划: 10.200.3.85 Kafka+ZooKeeper 10.200.3.86 Kafka+ZooKeeper 10.200.3.87 Kaf ...
- Nginx HTTP变量原理
L:72 首先如何获取url追加参数值 如: http://www.xxx.com?a=1&b=2 return '$arg_a, $arg_b'; #通过前缀 arg_a 就能获取到 参数a ...
- odoo 权限问题
odoo 权限问题 权限组问题 权限组是为了将人员按组划分同一分配权限.权限组的建立是基于每个应用来实现的 建立一个应用的分组(可省略,主要用于创建用户时有选择项) 建立一条record记录model ...
- hdu P3374 String Problem
今天又在lyk大佬的博客学会了——最小表示法(异常激动发篇题解纪念一下说在前面:给luogu提个建议最小表示法的题太少了,都被hdu抢去了!!! 我们先看一下题目 看完后可以用一个字概括——蒙,两个字 ...
- 简单 php 代码跟踪调试实现
简单 php 代码跟踪调试实现 debug_backtrace:生成回溯 debug_print_backtrace:打印回溯 1. debug_backtrace ($options = DEBUG ...