Conditional methods and their Parameter evaluation

In .NET the System.Diagnostics.ConditionalAttribute can be used for conditional compilation. A method can be marked with this attribute as follows

using System.Diagnostics;
static class Logger
static public void Info(string message)

This means that the Logger.Trace is compiled into IL only when the TRACE sumbol is defined either in the command line build as /define:TRACE or in the VS IDE in Project->Properties->Build->General->Conditional Compilation symbols. This makes ConditionalAttribute an excellent choice for logger and debug utilities. For example the logger can have the Info and verbose methods marked with [Conditional("Debug")] and hence they do not add to the burden of in release bits. Conditional attributes is a much better choice than #ifdefs as they are always syntax verified.

However, it brought up a very interesting discussion recently. Consider the following code

MyClass mc = new MyClass();

The question is does the mc.ToString evaluate when TRACE is not defined?? Or in other words are parameters of conditionally compiled methods evaluated? If it does then even for release bits overhead starts creeping in. The good news is that it doesn't. An easy way to verify is to override MyClass::ToString and put in a Console.WriteLine into it. This method will not be called if TRACE is not defined. Or you can just use ILDASM or .NET Reflector to see that the method call doesn't even go into the IL and hence there can be no overhead.

Comments (4)

  1. Dan Vallejo says:

    Actually, your first statement is incorrect.

    "Logger.Trace is compiled into IL only when the TRACE sumbol". Logger.Info is always compiled but calls to Logger.Info are made only when the TRACE symbol is defined.

  2. ... says:

    ..Rather helpful information you have here. Grazie!

  3. Just yesterday I was chatting with a friend and he was telling me how they are writing a thread-safe

Skip to main content