First, note that you cannot specify a normal delegate with the ConditionalAttribute method.
However, you use lambda to compile OK. But what does he actually compile?
Consider this code:
Action<string> print = message => Debug.WriteLine(message); print("TEST");
To build debugging, this compiles to:
Action<string> print = delegate (string message) { Debug.WriteLine(message); }; print("TEST");
To build the release, it compiles to:
Action<string> print = delegate (string message) { }; print("TEST");
In both cases, a delegate is created and called - so in both cases you will have the overhead of creating and calling a delegate (this parameter is pushed onto the stack), although the release version actually does nothing.
So, for your case, the difference between using #if DEBUG or not is as follows:
- If you use
#if DEBUG , then there is no overhead, and the Log property will not be set. - Otherwise, you have overhead when setting up the
Log property, and then it does nothing when it is called.
In many cases, the overhead is so small that you don't mind - and it's also pretty nice to ensure that the Log property is always set to something (the delegate doesnโt do anything by default) t you need to check it against zero before referencing on him every time.
Using #if DEBUG , on the other hand, makes things more clear. The way lambda interacts with the method defined with ConditionalAttribute is not completely obvious!
All this is a roundabout way of saying: weigh the pros and cons and make your choice .;)
source share