5

According to the accepted answer on this question:

When the application is compiled in the release configuration, 
the Debug elements will not be compiled into the code.

Do the argument expression evaluation side effects of Debug.WriteLine() (and similar) happen in release builds? I'm not sure what "Debug elements" really means.

Community
  • 1
  • 1
Mike
  • 2,429
  • 1
  • 27
  • 30
  • 1
    This is not really a question whose answer should matter. A Debug.WriteLine() argument should *never* have side effects beyond those necessary to do its job. Doing things like calling arbitrary functions and incrementing values causes the observable behavior of your Debug build to differ from your Release build, which makes debugging almost a fruitless exercise. – Cody Gray - on strike Jun 24 '16 at 16:28
  • 3
    C# specification **17.4.2.1 Conditional methods**: *If the symbol is defined, the call is included; otherwise, the call (**including evaluation of the receiver and parameters of the call**) is omitted.* – user4003407 Jun 24 '16 at 16:29
  • Having spent a much of my career debugging other's people code I think it is a question whose answer matters. – Mike Jun 24 '16 at 16:48

1 Answers1

6

This is easy enough to try yourself:

class Program
{
    static void Main(string[] args) {
        int i = 0;
        Debug.WriteLine(i++);
        Console.WriteLine(i);
        Console.ReadLine();
    }
}

In Debug Mode the console prints 1. In Release Mode the console prints 0.

helrich
  • 1,300
  • 1
  • 15
  • 34
  • 1
    empirical tests show the behavior of one release of one compiler, I was looking for something more like the comment from @PetSerAl – Mike Jun 24 '16 at 16:41