DEV Community

Zero Heartbeat
Zero Heartbeat

Posted on Originally published at delta1labs.com

How the Conditional attribute elides calls

Open a Release build of almost any real .NET application in a decompiler and you will eventually find a method that nothing calls. Its body is intact, its name is meaningful, and yet a call-graph search comes up empty. The instinct is to mark it dead. For a whole class of methods that instinct is wrong, and the proof is sitting one line above the signature: a [Conditional("...")] attribute the compiler deliberately left in the metadata. This post shows exactly what that attribute does to the IL, why the method survives while its callers vanish, and how Glass.NET surfaces the clue so you do not delete code that a Debug build still needs.

The real question

System.Diagnostics.ConditionalAttribute is the mechanism behind Debug.Assert, Debug.WriteLine and Trace.WriteLine — all of them are marked [Conditional("DEBUG")] or [Conditional("TRACE")]. Everyone knows those calls "disappear in Release." The interesting question is what disappears, and when the decision is made. The answer is precise and often misremembered: the method body is always compiled, and the calls to it are omitted — one call site at a time, decided at each caller's compile time by that caller's own preprocessor symbols. Nothing about the method itself is conditional.

What the source looks like

Here is a diagnostic method marked [Conditional("TRACE")] and a caller that uses it:

using System.Diagnostics;

static class Diagnostics
{
    [Conditional("TRACE")]
    public static void Log(string message)
    {
        System.Console.WriteLine($"[trace] {message}");
    }
}

static class Worker
{
    public static int Run(int[] data)
    {
        Diagnostics.Log($"processing {Describe(data)}");
        return data.Length;
    }

    static string Describe(int[] data) => $"{data.Length} items";
}
Enter fullscreen mode Exit fullscreen mode

Log is an ordinary void method. Run calls it, and — note this — passes an argument that itself calls Describe. That argument expression is the part most people forget about.

What the compiler emits

Compile with TRACE defined (the default for a Debug build in the standard templates) and Run contains a real call. Watch for the call instruction and the work that builds its argument:

.method public hidebysig static int32 Run (int32[] data) cil managed
{
    IL_0000: ldstr      "processing "
    IL_0005: ldarg.0
    IL_0006: call       string Worker::Describe(int32[])
    IL_000b: call       string [System.Runtime]System.String::Concat(string, string)
    IL_0010: call       void Diagnostics::Log(string)
    IL_0015: ldarg.0
    IL_0016: ldlen
    IL_0017: conv.i4
    IL_0018: ret
}
Enter fullscreen mode Exit fullscreen mode

Now compile the identical source without TRACE defined (a stock Release build). The same method:

.method public hidebysig static int32 Run (int32[] data) cil managed
{
    IL_0000: ldarg.0
    IL_0001: ldlen
    IL_0002: conv.i4
    IL_0003: ret
}
Enter fullscreen mode Exit fullscreen mode

The call void Diagnostics::Log(string) is gone — and so is everything that produced its argument. There is no ldstr, no call Describe, no String::Concat. The compiler did not blank out the call and leave the argument setup behind; it removed the entire statement as a unit. Describe is never invoked. If Describe had incremented a counter or written to a log, that side effect is simply absent from the Release build. This is the single most important practical fact about [Conditional]: arguments to an elided call are not evaluated.

The method itself is untouched

Crucially, Log is still in the Release assembly, compiled exactly as written, Console.WriteLine and all. [Conditional] never touched the callee. And the attribute itself is preserved in metadata on Log:

.method public hidebysig static void Log (string message) cil managed
{
    .custom instance void [System.Runtime]System.Diagnostics.ConditionalAttribute::.ctor(string)
        = (01 00 05 54 52 41 43 45 00 00)   // "TRACE"
    // ... body intact ...
}
Enter fullscreen mode Exit fullscreen mode

So the Release assembly contains a fully-defined Log method with a ConditionalAttribute on it and zero callers anywhere in the assembly. That combination — a live method body, no inbound calls, and a conditional attribute — is the fingerprint.

How this differs from #if DEBUG

It is easy to conflate [Conditional] with #if, but they operate at different stages and in opposite directions.


One call site compiles to a call under DEBUG builds and to nothing under Release, while the method and its attribute remain in the assembly





The same call site compiles to a call under a DEBUG/TRACE build and to nothing under Release — arguments included — while the method body and its ConditionalAttribute stay in the assembly.

#if DEBUG is a preprocessor directive: it deletes source text before the compiler ever sees it. If the symbol is undefined, the code inside the block does not exist — it cannot be called because it was never compiled. [Conditional] works after compilation has begun and does the inverse: the method is compiled unconditionally and only the calls are suppressed. The decision is made per call site, using the symbols defined where the caller is compiled — which is why a Release assembly can reference a Debug-only helper in another assembly and have every call to it elided without touching that other assembly at all.

Reading it in Glass

A decompiler that silently dropped the attribute would hand you a puzzle: a well-named method, clearly intended to run, that nothing invokes. You would be one "remove unused code" refactor away from breaking the Debug build. Glass.NET renders the method with its attribute intact:

[Conditional("TRACE")]
public static void Log(string message)
{
    Console.WriteLine($"[trace] {message}");
}
Enter fullscreen mode Exit fullscreen mode

That single line is the whole explanation. The moment you see [Conditional("TRACE")], the "zero callers" result stops being a mystery: the calls were elided at compile time in this build because TRACE was not defined, and they are present in a build where it is. Glass keeps custom attributes like this one because they are not decoration — they are the metadata that makes the surrounding reality legible.

Caveats and where it breaks

  • Void methods only. The C# compiler rejects [Conditional] on a method that returns a value, because eliding a call that produces a value would leave a hole on the evaluation stack. The attribute is also honoured on attribute classes, where it conditionally includes the attribute's own emission — a separate use of the same mechanism.
  • A decompiled Release assembly cannot tell you what calls would have existed. The elision happened at the caller's compile time and left no trace at the call site. You can see that Log is conditional and uncalled; you cannot recover where, or how often, it would have been called in a TRACE build. That information is simply not in the binary.
  • "No callers" is not "dead." Removing a [Conditional] method changes the meaning of the source and breaks any build that defines the symbol. Treat the attribute as a hold on the method, not a green light to delete.
  • Multi-symbol conditions are OR, not AND. A method may carry several [Conditional] attributes — [Conditional("DEBUG")] [Conditional("TRACE")]. The call is emitted if any of the named symbols is defined at the call site. Reading only the first attribute and assuming it is the whole condition will mislead you.

Takeaway

[Conditional] is a rare case where the compiler removes callers rather than callees, and it does the removal argument-and-all, caller by caller, using each caller's own symbols. The method, its body and its attribute all survive into the assembly. When a decompiled Release binary shows a defined method with no callers, the [Conditional("...")] attribute is the one piece of metadata that explains it — which is why a decompiler worth trusting shows the attribute rather than quietly dropping it. Glass.NET is a free .NET decompiler for inspecting code you have the right to inspect; preserving signals like this is the difference between reading an assembly and misreading it.

Top comments (0)