Will the expression provided to Debug.Assert be evaluated in a release build?

拈花ヽ惹草 提交于 2019-12-22 08:11:40

问题


Say I have a rather expensive assertion:

bool IsCompatible(Object x, Object y) { 
  // do expensive stuff here
}

If I test this assertion with:

Debug.Assert(IsCompatible(x,y));

Will IsCompatible be executed in release builds?

My understanding is that Debug.Assert being marked as [Conditional("DEBUG")], calls to it will only be emitted in debug builds. I'm thinking that this won't prevent the expression from being evaluated in release mode though, since the method call may have side effects, only the passing of the result to Debug.Assert wouldn't be emitted. Is that correct?

Should I be doing:

#if DEBUG
Debug.Assert(IsCompatible(x,y));
#endif

To ensure that I don't pay the cost of IsCompatible in release mode?


回答1:


No.

Debug.Assert is decorated with [Conditional("DEBUG")].

When the compiler compiles a call to this method, it will remove that entire statement unless DEBUG is defined in the calling project.



来源:https://stackoverflow.com/questions/13383651/will-the-expression-provided-to-debug-assert-be-evaluated-in-a-release-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!