I'm aware that you can use #if DEBUG
and the likes in C#, but is it possible to create a method, or class, that is ignored completely, including all usages that are not wrapped inside an #if DEBUG
block?
Something like:
[DebugOnlyAttribute]
public void PushDebugInfo()
{
// do something
Console.WriteLine("world");
}
and then:
void Main()
{
Console.WriteLine("hello ");
Xxx.PushDebugInfo();
}
which, if DEBUG
is defined will print "hello world", otherwise just "hello ". But more importantly, the MSIL should not contain the method-call at all in release builds.
I believe the behavior I'm after is something similar to Debug.WriteLine, whose call is completely removed and has no impact on performance or stack depth in release builds.
And, if possible in C#, would any .NET language using this method behave the same (i.e., compile-time vs run-time optimization).
Also tagged f#, because essentially I will need this method there.
It seems like you're looking for ConditionalAttribute
.
For example, this is a portion of Debug
class source code:
static partial class Debug
{
private static readonly object s_ForLock = new Object();
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert(bool condition)
{
Assert(condition, string.Empty, string.Empty);
}
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert(bool condition, string message)
{
Assert(condition, message, string.Empty);
}
................................