# Why decompiled constants look pre-computed


Open a compiled assembly in a decompiler, find a method you wrote with a nice readable expression like `60 * 60 * 24`, and the decompiler stares back with `86400`. The arithmetic is gone. So is the string concatenation you wrote as `"v" + "1" + ".0"` — it comes back as a single `"v1.0"`. This is not a decompiler weakness or a missing feature. The information you are looking for was deleted by the C# compiler before the assembly was ever written, and no decompiler on earth can recover it.

This is one of the clearest examples of a general truth about decompilation: a decompiler reconstructs *equivalent* source from IL, not your *original* source. Constant folding is where the two diverge most visibly. Understanding it tells you exactly which parts of your code survive the round trip and which are lost for good.

## The real question

When is an expression evaluated — at compile time, or at run time? For anything the compiler can prove is constant, the answer is compile time. The C# language specification requires a *constant expression* to be fully evaluated during compilation, and only the result is emitted. The operands and the operator never reach the IL. A decompiler reading that IL has literally no bytes describing `60`, `*`, or `24`; it has one integer, `86400`, and the only honest thing it can print is `86400`.

## The mechanism, shown

Take a declaration and a use site:

```csharp
public static class Time
{
    public const int SecondsPerDay = 60 * 60 * 24;
}

public long BudgetFor(int days) => days * Time.SecondsPerDay;
```

The compiler evaluates `60 * 60 * 24` once, during compilation, producing `86400`. That value is stored in metadata as the field's constant, and it is *inlined* at every use site. Here is the IL for `BudgetFor`:

```
.method public instance int64 BudgetFor(int32 days)
{
  IL_0000: ldarg.1
  IL_0001: ldc.i4    86400
  IL_0006: conv.i8
  IL_0007: mul
  IL_0008: ret
}
```

Note what is *not* there. There is no reference to `Time.SecondsPerDay`, no field load, no multiplication of `60 * 60 * 24`. The `ldc.i4 86400` is a single "load the integer 86400" instruction, and the `mul` that follows belongs to `days * …`, not to the folded constant. The const field was never read at run time; its value was copied straight into the instruction stream.

<figure class="my-8">
<svg viewBox="0 0 680 250" role="img" aria-label="The source expression 60 times 60 times 24 is folded by the compiler into the literal 86400, emitted as ldc.i4 86400 in the IL, and a decompiler can only print 86400" xmlns="http://www.w3.org/2000/svg" style="max-width:680px;width:100%;height:auto;font-family:system-ui,sans-serif;font-size:13px">
<text x="340" y="26" text-anchor="middle" fill="#64748b" font-weight="bold">What the compiler keeps, and what it throws away</text>
<rect x="24" y="56" width="150" height="48" rx="6" fill="none" stroke="#6366f1" stroke-width="2"/>
<text x="99" y="80" text-anchor="middle" fill="#6366f1" font-weight="bold">source</text>
<text x="99" y="98" text-anchor="middle" fill="#6366f1">60 * 60 * 24</text>
<rect x="222" y="56" width="150" height="48" rx="6" fill="#6366f1" fill-opacity="0.12" stroke="#6366f1" stroke-width="2"/>
<text x="297" y="80" text-anchor="middle" fill="#6366f1" font-weight="bold">compiler folds</text>
<text x="297" y="98" text-anchor="middle" fill="#6366f1">evaluate once</text>
<rect x="420" y="56" width="150" height="48" rx="6" fill="none" stroke="#0891b2" stroke-width="2"/>
<text x="495" y="80" text-anchor="middle" fill="#0891b2" font-weight="bold">IL emitted</text>
<text x="495" y="98" text-anchor="middle" fill="#0891b2">ldc.i4 86400</text>
<rect x="420" y="160" width="150" height="48" rx="6" fill="none" stroke="#10b981" stroke-width="2"/>
<text x="495" y="184" text-anchor="middle" fill="#10b981" font-weight="bold">decompiled</text>
<text x="495" y="202" text-anchor="middle" fill="#10b981">86400</text>
<line x1="174" y1="80" x2="222" y2="80" stroke="#64748b" stroke-width="2" marker-end="url(#ah)"/>
<line x1="372" y1="80" x2="420" y2="80" stroke="#64748b" stroke-width="2" marker-end="url(#ah)"/>
<line x1="495" y1="104" x2="495" y2="160" stroke="#64748b" stroke-width="2" marker-end="url(#ah)"/>
<text x="150" y="150" text-anchor="middle" fill="#64748b">operands and the</text>
<text x="150" y="168" text-anchor="middle" fill="#64748b">* operator are</text>
<text x="150" y="186" text-anchor="middle" fill="#64748b">discarded here —</text>
<text x="150" y="204" text-anchor="middle" fill="#64748b">never written to disk</text>
<line x1="297" y1="104" x2="230" y2="150" stroke="#64748b" stroke-width="1.5" stroke-dasharray="4 4" marker-end="url(#ah)"/>
<defs><marker id="ah" markerWidth="8" markerHeight="8" refX="6" refY="3" orient="auto"><path d="M0,0 L6,3 L0,6 Z" fill="#64748b"/></marker></defs>
</svg>
<figcaption class="text-sm text-muted mt-2">The compiler evaluates the constant expression once and emits only the result. By the time the IL exists, the operands and the operator are gone — the decompiler sees a single literal and can print nothing else.</figcaption>
</figure>

### Where the const value is actually stored

The declaration `SecondsPerDay` is not completely erased. It lives in the metadata as a `Field` row whose value is held in the **Constant** table. A constant row pairs a parent (the field) with a type tag and the raw little-endian value bytes:

```
Field    : SecondsPerDay   flags: Public Static Literal   signature: int32
Constant : parent=Field(SecondsPerDay)  type=ELEMENT_TYPE_I4  value=0x00015180
```

`0x00015180` is `86400`. Because the field name is preserved, a decompiler reading the *declaring* type can and does reconstruct `public const int SecondsPerDay = 86400;` — name intact, expression lost. Glass.NET shows exactly this. What it cannot show is `60 * 60 * 24`, because that text was consumed by the compiler and never recorded anywhere.

### Strings fold too

The same rule applies to string constants. A compile-time concatenation of literals is evaluated during compilation and the single joined result is interned into the `#US` (user-strings) heap:

```csharp
const string Banner = "Glass" + ".NET" + " eval";
```

```
IL_0000: ldstr   "Glass.NET eval"
```

There is one `ldstr` pointing at one `#US` entry. The three source fragments and the `+` operators are gone. The decompiler prints `"Glass.NET eval"`.

### Enums are just folded integers

An enum constant is a named integer, and the name is a compile-time convenience. In the IL, an enum value folds to its underlying integer literal:

```csharp
enum Level { Off, Low = 10, High = 20 }
int x = (int)Level.High;   // → ldc.i4.s 20
```

A good decompiler re-maps `20` back to `Level.High` *when it can see the enum type and the context demands it* — but the IL itself carries only `20`. Where the type information is ambiguous, you get the bare number.

## How this looks in Glass.NET

[Glass.NET](https://delta1labs.com/products/glass) is a free .NET decompiler, and reading your own or an unfamiliar assembly with it, you will constantly meet folded values. The practical skill is telling a folded constant apart from a value computed at run time. The test is simple and lives in the IL, which Glass.NET shows alongside the reconstructed C#:

- **A folded constant is a single load.** `ldc.i4`, `ldc.i8`, `ldc.r8`, or `ldstr`, with no arithmetic opcodes feeding it. The compiler did the work; the IL carries only the answer.
- **A runtime computation keeps its operators.** You see `ldarg`/`ldloc`/`ldsfld` loads followed by `mul`, `add`, `conv`, and friends. The operands are real values that are not all known until run time.

So when you decompile a method and see a lone `86400` or `0.01745329` or `"v1.0"` with no visible arithmetic, you are looking at something the author wrote as an expression that the compiler folded. The readable form is recoverable only from the original source or from a const field's surviving name — not from the use site.

## Caveats, and when this breaks down

Be precise about the limits:

- **The expression is unrecoverable.** This is the hard boundary. No decompiler recovers `60 * 60 * 24` from `86400`, because the inputs are not in the file. Any tool that claimed to would be guessing.
- **Named consts survive at their declaration, not at use sites.** The Constant table keeps the field *name*, so the declaration decompiles cleanly. But every consumer inlined the literal, so use sites read as magic numbers with no link back to `SecondsPerDay`. The name-to-use-site relationship is severed at compile time.
- **Folded values look like magic numbers.** A `3600` deep inside a method gives no hint it once meant "seconds per hour". This is a readability cost of folding, not a decompiler failure.
- **Not everything folds.** Only *constant expressions* — those whose operands are all compile-time constants — are folded. `days * SecondsPerDay` is not folded, because `days` is a runtime value; only the `SecondsPerDay` half is a literal. `static readonly` fields are *not* constants: they are evaluated at run time in the type initializer, so they are genuine field loads (`ldsfld`) and the decompiler shows the field access, not an inlined value.
- **Conditional compilation is a separate mechanism.** `#if` directives and `[Conditional]` methods drop code at compile time too, but by a different route — they are not constant folding, and they leave their own distinct traces (or absences). That is a topic for another post.

## Takeaway

A decompiler is an IL-to-C# reconstruction, not a time machine to your source. Constant folding draws the sharpest line between the two: the compiler evaluates every constant expression once, writes only the result as a literal (`ldc.i4 86400`, `ldstr "v1.0"`), and keeps a const's *name* in the Constant table while discarding its *expression* entirely. When Glass.NET shows you `86400`, that is not lossy decompilation — it is a faithful reading of all that is left. Learn to read the IL beside the C#, and a lone literal with no arithmetic tells you instantly that you are seeing a folded constant, and that the readable expression behind it lives only in the original source. For more on how Glass.NET reconstructs IL into C#, see the [documentation](https://delta1labs.com/docs/glass/).

