One of the less known features in .NET is conditional methods, which allow developers to create methods whose calls are included or excluded during compilations based on a preprocessing symbol.
Say, you want to execute a certain method only under certain conditions known at compilation time. In the “old days”, you’d use #if…#endif pre-processor definitions. In .NET, you can use the System.Diagnostic.ConditionalAttribute class to do the same:
private void Method1()
. . .
. . .
// NOTE: Make sure to define MYCONDITION conditional compilation symbol in
// project properties -> build tab
private void Method2()
In effect, of MYCONDITION is not defined, Method2 call is a no-op. Most commonly used condition is DEBUG…
BEWARE: One would wish that the following would be allowed:
// TODO: add debug version specific code
// TODO: add release version code
But, it will not compile. It’s unfortunate, especially since the following compiles without any problems:
// TODO: add debug version code
Well, may be in .NET 3.0?