Skip to content
← All posts
· Delta1 Labs Glass.NETDecompilation

Why decompiled constants look pre-computed

A decompiler shows 86400 where you wrote 60 * 60 * 24, because the C# compiler folds constant expressions at compile time and emits only the result. How const evaluation, propagation and the Constant metadata table work — and what Glass.NET can and cannot recover.

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:

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.

What the compiler keeps, and what it throws away source 60 * 60 * 24 compiler folds evaluate once IL emitted ldc.i4 86400 decompiled 86400 operands and the * operator are discarded here — never written to disk
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.

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:

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:

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 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.

Try Nebula.NET

Harden your .NET code in minutes — start with the free edition.