Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Debug.Assert generate IL in release mode?

Tags:

c#

.net

il

When Debug.Assert() method calls exist in source code and I compile in release mode, does the compiler generate the IL for the Debug.Assert() even though it's not called?

One of our developers added an Assert recently that displays information about our internal security. Could someone look at the release mode IL and figure out the text for the assert?

like image 839
Osiris Avatar asked Apr 03 '12 15:04

Osiris


People also ask

Does debug assert work in Release mode?

Assert works only in DEBUG mode - or, at least, when the DEBUG variable is defined. Otherwise, all those checks will simply get removed from the build result, so they will not impact your application when running in RELEASE mode.

What is the use of debug assert?

Assert(Boolean, Debug+AssertInterpolatedStringHandler) Checks for a condition; if the condition is false , outputs a specified message and displays a message box that shows the call stack.

Can asserts help with debugging?

Debugging Your Code With Assertions. At its core, the assert statement is a debugging aid for testing conditions that should remain true during your code's normal execution. For assertions to work as a debugging tool, you should write them so that a failure indicates a bug in your code.

How does assert work c#?

Assert allows you to assert a condition (post or pre) applies in your code. It's a way of documenting your intentions and having the debugger inform you with a dialog if your intention is not met. Unlike a breakpoint, the Assert goes with your code and can be used to add additional detail about your intention.


2 Answers

It does not by default, unless you define the DEBUG symbol (and by default, for Release that is turned off).

To verify, open your Project Properties and select the Build pane in Visual Studio. It will show the checkbox "Define DEBUG constant". If it is turned on for Release, then asserts will fire; otherwise, they won't.

like image 123
Roy Dictus Avatar answered Oct 11 '22 10:10

Roy Dictus


No, the members of the Debug class (with the ConditionalAttribute attribute) do not emit IL. There is no explicit mention on MSDN, however the following two quotes imply the behaviour quite well, so to augment Roy's answer:

If you use methods in the Debug class to print debugging information and check your logic with assertions, you can make your code more robust without affecting the performance and code size of your shipping product.

So, no size difference implies no output from these whatsoever, and

The ConditionalAttribute attribute is applied to the methods of Debug. Compilers that support ConditionalAttribute ignore calls to these methods unless "DEBUG" is defined as a conditional compilation symbol. Refer to a compiler's documentation to determine whether ConditionalAttribute is supported and the syntax for defining a conditional compilation symbol.

Which means that, at the compiler level, these calls won't even be considered (when DEBUG is not defined.)

like image 45
Grant Thomas Avatar answered Oct 11 '22 08:10

Grant Thomas