C#.NET 3.5中定义的Assert在哪里?

时间:2014-10-28 09:31:57

标签: c# assert assertions

我发现了很多材料如何使用它,但不是什么 使用XXX.YYY.ZZZ;我应该使用指令。甚至没有在MSDN页面上......

我一直收到错误:

  

"名称'断言'在当前上下文中不存在"

那么我应该声明我正在使用什么包?

非常感谢提前!

2 个答案:

答案 0 :(得分:2)

假设您在代码中寻找Asserts,而不是单元测试:

System.Diagnostics.Debug.Assert

答案 1 :(得分:1)

由于使用Assert Assert(a !=b)的注意事项似乎存在很多混淆,以及如何使用using System; using System.Diagnostics; class Program { static void Main() { int a = 5, b = 10; Console.WriteLine("hello"); Debug.Assert(a != b); // should get past this Console.WriteLine("world"); b = 5; Debug.Assert(a != b); // should fail in debug mode } } 的注释中的C示例,这是一个完全有效的示例(应该编译得很好等) }:

Debug.Assert

请注意,这使用常规运行时断言,而不是任何特定/任意测试框架。请注意,因为[Conditional("DEBUG")]是{{1}}方法,所以不会为发布版本调用它。