Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop evaluation of parameters in calls to debug functions when in Release build (C#)

I have some code which is littered with debug expressions similar to:

System.Diagnostics.Debug.WriteLine(System.BitConverter.ToString(data.Take(32).ToArray()));

What can I do to prevent the Release version evaluating System.BitConverter.ToString(data.Take(32).ToArray())?

I have added my own debug class that allows me to control calls to WriteLine() based on the level of reporting I want but both methods evaluate the Linq and string conversion even in Release mode (don't they)?

---ADDED LATER---

As I said above there are many of these lines in the code and I don't particularly want to #if DEBUG them all out.

What I want is to know how to avoid evaluating the Linq and the BitConverter, which I assume will be a performance hit, in Release mode.

like image 858
adrianwadey Avatar asked Dec 14 '22 13:12

adrianwadey


1 Answers

You do not have to do anything! The evaluation of the parameters passed to a method that is removed because of the ConditionalAttribute is suppressed already.

See the section The Conditional attribute in the C# Language Specification for authoritative documentation. Quote:

[...] the call (including evaluation of the parameters of the call) is omitted.

To see that the method actually carries the ConditionalAttribute, see Debug.WriteLine(string) documentation.

like image 68
Jeppe Stig Nielsen Avatar answered Mar 30 '23 00:03

Jeppe Stig Nielsen