Debug类提供了几种用于调试代码的方法。其Assert方法采用布尔值,如果值为false则抛出异常。第二个参数给出异常应显示的错误消息。如果在调试器中运行时断言失败,您可以选择打开调试器到抛出异常的 Debug.Assert语句。
通常,您使用Debug.Assert来验证代码中的条件(例如输入和输出值)。例如,以下Average方法使用Debug.Assert来验证方法的输入参数。
// Return the average of the numbers.
private float Average(float[] values)
{
Debug.Assert(values != null,
"Values array cannot be null");
Debug.Assert(values.Length > 0,
"Values array cannot be empty");
Debug.Assert(values.Length < 100,
"Values array should not contain more than 100 items");
// If there are no valu