10

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?

Yves M.
  • 29,855
  • 23
  • 108
  • 144
Osiris
  • 489
  • 3
  • 14
  • Check out my [old blog entry](http://tjoe.wordpress.com/2007/12/03/cotw-conditionalattribute-class/), keeping in mind that the `Debug` methods are decorated with `[Conditional("DEBUG")]`. – CodeNaked Apr 03 '12 at 15:08

2 Answers2

10

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.)

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • The MSDN does explicitly show the attribute on the [method level documentation](http://msdn.microsoft.com/en-us/library/kssw4w7z.aspx). It does not say what the attribute does but you could always look up the documentation on the attribute itself. – Scott Chamberlain Dec 06 '14 at 17:21
9

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.

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76