Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

From https://docs.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...