Trace.Assert vs Debug.Assert [英] Trace.Assert vs Debug.Assert

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

问题描述

我正在调试计算贷款利息的应用程序。该应用程序包括以下代码。

  private   static   decimal  CalculateInterest( decimal  loanAmount, int  loanTerm, decimal  loanRate)
{
decimal interestAmount = loanAmount * loanRate * loanTerm;
// 需要在此处插入Assert方法
return interestAmount;我需要确保当loanAmount变量在所申请。

我该怎么办?

A.插入以下代码段:

Trace.Assert( loanAmount> 0);

B.插入以下代码段:

Debug.Assert(loanAmount> 0);

解决方案

来自关于Debug.Assert的MSDN页面 [ ^ ]:

Quote:

默认情况下,Debug.Assert方法仅适用于调试版本。如果你想在发布版本中做断言,请使用Trace.Assert方法。



所以在你的情况下,你可能需要Debug.Assert。


Trace.Assert,因为它表示所有构建配置

 Debug.Assert方法消失 时创建一个Release您的代码版本


I am debugging an application that calculates loan interest. The application includes the following code.

private static decimal CalculateInterest(decimal loanAmount, int loanTerm, decimal loanRate)
{
     decimal interestAmount = loanAmount * loanRate * loanTerm;
     // need to insert Assert method here
     return interestAmount;
}


I need to ensure that the debugger breaks execution within the CalculateInterest() method when the loanAmount variable is less than or equal to zero in all builds of the application.
What should I do?
A. Insert the following code segment:
Trace.Assert(loanAmount > 0);
B. Insert the following code segment:
Debug.Assert(loanAmount > 0);

解决方案

From the MSDN page about Debug.Assert[^]:

Quote:

By default, the Debug.Assert method works only in debug builds. Use the Trace.Assert method if you want to do assertions in release builds.


So in your case, you probably want Debug.Assert.


Trace.Assert since it says all build configurations

Debug.Assert method disappear when you create a Release version of your code


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

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