# 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](https://delta1labs.com/products/glass) 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:

```csharp
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";
}
```

`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:

```text
.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
}
```

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

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

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`:

```text
.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 ...
}
```

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.

<figure class="my-8">
<svg viewBox="0 0 780 320" role="img" aria-labelledby="cond-title" xmlns="http://www.w3.org/2000/svg" style="max-width:780px;width:100%;height:auto">
<title id="cond-title">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</title>
<defs><marker id="condarrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0 0L10 5L0 10z" fill="#6366f1"/></marker></defs>
<rect x="18" y="120" width="214" height="92" rx="10" fill="none" stroke="#6366f1" stroke-width="1.5"/>
<text x="34" y="146" fill="#6366f1" font-family="sans-serif" font-size="13" font-weight="700">Source call site</text>
<text x="34" y="172" fill="#64748b" font-family="monospace" font-size="12">Diagnostics.Log(</text>
<text x="34" y="190" fill="#64748b" font-family="monospace" font-size="12">  Describe(data));</text>
<rect x="300" y="28" width="236" height="92" rx="10" fill="none" stroke="#0891b2" stroke-width="1.5"/>
<text x="316" y="54" fill="#0891b2" font-family="sans-serif" font-size="13" font-weight="700">TRACE defined (Debug)</text>
<text x="316" y="80" fill="#64748b" font-family="monospace" font-size="12">call Describe</text>
<text x="316" y="100" fill="#64748b" font-family="monospace" font-size="12">call Log  // emitted</text>
<rect x="300" y="208" width="236" height="92" rx="10" fill="none" stroke="#10b981" stroke-width="1.5"/>
<text x="316" y="234" fill="#10b981" font-family="sans-serif" font-size="13" font-weight="700">TRACE undefined (Release)</text>
<text x="316" y="260" fill="#64748b" font-family="monospace" font-size="12">// no call, no args</text>
<text x="316" y="280" fill="#64748b" font-family="monospace" font-size="12">Describe not run</text>
<rect x="566" y="118" width="196" height="96" rx="10" fill="none" stroke="#6366f1" stroke-width="1.5"/>
<text x="582" y="144" fill="#6366f1" font-family="sans-serif" font-size="13" font-weight="700">In the assembly</text>
<text x="582" y="170" fill="#64748b" font-family="monospace" font-size="12">Log { ... } defined</text>
<text x="582" y="190" fill="#64748b" font-family="monospace" font-size="11">[Conditional("TRACE")]</text>
<text x="582" y="206" fill="#64748b" font-family="sans-serif" font-size="11">0 callers · attr kept</text>
<line x1="232" y1="150" x2="298" y2="92" stroke="#6366f1" stroke-width="1.5" marker-end="url(#condarrow)"/>
<line x1="232" y1="182" x2="298" y2="246" stroke="#6366f1" stroke-width="1.5" marker-end="url(#condarrow)"/>
<line x1="536" y1="254" x2="588" y2="212" stroke="#10b981" stroke-width="1.5" marker-end="url(#condarrow)"/>
<line x1="536" y1="74" x2="588" y2="150" stroke="#0891b2" stroke-width="1.5" marker-end="url(#condarrow)"/>
</svg>
<figcaption class="text-sm text-muted mt-2">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.</figcaption>
</figure>

`#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:

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

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.

