Conditional Methods
I saw an internal post today about somebody who wanted to get rid of their #if DEBUG statements in their code, because they were ugly. That made me realize that there's a feature that not everybody knows about, known as conditional methods.
Consider the following code:
using System;
using System.Diagnostics;
class Program {
static void Main(string[] args) {
#if DEBUG
DebugMethod1();
#endif
DebugMethod2();
Console.ReadKey();
}
static void DebugMethod1() {
Console.WriteLine("Debug1");
}
[Conditional("DEBUG")]
static void DebugMethod2() {
Console.WriteLine("Debug2");
}
}
In my call to DebugMethod1(), I've used the traditional #if approach to only compile the call if the DEBUG symbol is defined. But in the call to DebugMethod2(), I've used a conditional method that has the same effect - if the DEBUG symbol is not defined, it's as if the call isn't there. That includes any side effects from parameter evaluation - they don't happen.
Note that the docs on the ConditionalAttribute class are a bit misleading.
Comments
- Anonymous
August 13, 2004
can you reformat the code to be on multiple lines? Feel free to reject this comment, of course :) - Anonymous
August 13, 2004
The only problem with that is that the conditional method gets compiled and placed in the assembly, regardless of the attribute.
That's too big of a security risk.
I'd rather stay with #if's. - Anonymous
August 14, 2004
I haven't seen this, but frankly that still seems like too much work considering your method must likely be void and there is no straightforward way to track what happened. So it looks handy, but limited in purpose.
There's some middle ground..
public static void Main(string[] args) {
// debuggy thing 1:
// so much cleaner
if (Debug) DoSomethingDebuggy();
// debuggy thing 2
// good for debug-specific environment variables
int special = SpecialProperty;
}
public int SpecialProperty {
get {
if (Debug) return 3;
return 7;
}
}
public static bool Debug {
#if DEBUG
return true;
#else
return false;
#endif
} - Anonymous
August 14, 2004
Typo in there, forgot the get {} in the Debug property, but you get the idea. shrug - Anonymous
August 14, 2004
Granted, throwing in a runtime conditional degrades performance. To the extent that for some people that actually matters, I retract. - Anonymous
August 15, 2004
Eric -
thanx
I didn't know about conditional - very good! - Anonymous
August 16, 2004
I would hope that the runtime conditional would only degrade performance during the JIT-compile phase. In native code, it shouldn't exist if it will never be used.
Can anyone confirm or deny this? - Anonymous
August 16, 2004
There is no runtime conditional - it's all handled at compile time. - Anonymous
August 19, 2004
The documentation states that the Conditional attribute determines whether the method is called or not. This implies that the method's code is still compiled, regardless of the attribute. For a public method, this could lead to unwanted, un-obfuscatable symbols in the application.
Also, I'd be interested to know the reason why it only works on void methods and not methods with return values or properties. - Anonymous
December 18, 2004
Helpful For MBA Fans. - Anonymous
December 27, 2004
[http://itpeixun.51.net/][http://aissl.51.net/][http://kukuxz003.freewebpage.org/][http://kukuxz001.51.net/][http://kukuxz003.51.net/][http://kukuxz005.51.net/][http://kukuxz002.51.net/][http://kukuxz004.freewebpage.org/][http://kukuxz007.51.net/][http://kukuxz001.freewebpage.org/][http://kukuxz006.51.net/][http://kukuxz002.freewebpage.org/][http://kukuxz004.51.net/][http://kukuxz008.51.net/][http://kukuxz009.51.net/][http://kukuxz005.freewebpage.org/][http://kukuxz006.freewebpage.org/][http://kukuxz007.freewebpage.org/][http://kukuxz009.freewebpage.org/]