Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does using ConditionalAttribute also remove arguments computation?

I tried the following code:

class Magic {
   [Conditional("DEBUG")]
   public static void DoMagic( int stuff )
   {
   }
   public static int ComputeMagic()
   {
      throw new InvalidOperationException();
   }
}
class Program
{
   static void Main(string[] args)
   {
      Magic.DoMagic(Magic.ComputeMagic());
   }
}

and it looks like in Release build the exception is not thrown so not only the call to a method marked with ConditionalAttribute is removed, but also the parameters computation is eliminated.

Is such behavior guaranteed?

like image 438
sharptooth Avatar asked Feb 21 '14 13:02

sharptooth


2 Answers

Yes, everything is removed. This can be great for avoiding expensive computations that are only relevant for (say) logging, but it means you need to be careful not to remove necessary side-effects. For example:

Trace(SomethingCritical());

is very different to:

var result = SomethingCritical();
Trace(result);

or in your specific case:

DoMagic(ComputeMagic()); // everything here done only if DEBUG symbol defined

versus

var result = ComputeMagic(); // always done
DoMagic(result); // done only if DEBUG symbol defined

From the specification §17.4.2 (emphasis mine):

If the symbol is defined, the call is included; otherwise, the call (including evaluation of the receiver and parameters of the call) is omitted.

like image 175
Marc Gravell Avatar answered Oct 21 '22 05:10

Marc Gravell


Yes, argument evaluation is removed when DEBUG is not defined (which is typical in Release builds).

From https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/attributes#reserved-attributes:

If the symbol is defined, the call is included; otherwise, the call (including evaluation of the parameters of the call) is omitted.

like image 20
AlexD Avatar answered Oct 21 '22 06:10

AlexD