4

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.

adrianwadey
  • 1,719
  • 2
  • 11
  • 17
  • Are you asking how to have certain code only execute in Debug? If so, that's [already been answered](http://stackoverflow.com/questions/3409598/need-net-code-to-execute-only-when-in-debug-configuration) – SpaceSteak Jul 08 '15 at 12:35
  • You could place a `#if DEBUG` directive around the according sections – LInsoDeTeh Jul 08 '15 at 12:35
  • It's only the internals of `Debug.WriteLine` that are not executed in release mode, the method is still called which is why the evaluation happens. You would either need to wrap these lines in `#if DEBUG...` (as mentioned by @LInsoDeTeh) or create your own method that decides what to output. – DavidG Jul 08 '15 at 12:37
  • If you want to avoid the avaluation of the expression, a logging framework (or your own logging code) won't help. You only option is conditional compilation, which as @SpaceSteak says is covered elsewhere (so voting to close this as a duplicate). – David Arno Jul 08 '15 at 12:43
  • The answer referred to does explain various options for conditional compilation but it doesn't explain whether `[Conditional("DEBUG")]` affects the evaluation of parameters being passed in the call. So it didn't answer my question (or at least the question I was trying to ask). – adrianwadey Jul 08 '15 at 13:26

3 Answers3

9

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.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
3

The answers and comments prompted me to test my supposition that the conversion is evaluated in Release mode.

Console.WriteLine("Console:{0}", ++n);
System.Diagnostics.Debug.WriteLine("Debug:{0}", ++n);
Console.WriteLine("Console:{0}", ++n);

In Release mode I get the output:

Console:1
Console:2

So the Debug line isn't being evaluated at-all in Release mode and I don't need to do any more than use the Debug output and/or add [Conditional("DEBUG")] to my own debug routines (which is already an attribute of the library Debug code).

[Conditional("DEBUG")] - although an attribute of the called function, prevents the function call which prevents the evaluation of parameters to it. It acts in a way very similar to #if DEBUG

adrianwadey
  • 1,719
  • 2
  • 11
  • 17
0

#if DEBUG should solve it to you:

#if DEBUG
System.Diagnostics.Debug.WriteLine(System.BitConverter.ToString(data.Take(32).ToArray()));
#endif

More examples here.

jcs
  • 611
  • 2
  • 9
  • 22