# Recovering checked user-defined operators


C# 11 added a small piece of syntax with a surprising amount of machinery behind it. A type can now declare what `+` does when the surrounding code is overflow-checked, separately from what it does by default:

```csharp
public static Amount operator +(Amount a, Amount b) => /* wraps */;
public static Amount operator checked +(Amount a, Amount b) => /* throws */;
```

Open an assembly that uses this in a decompiler that predates the feature and you get a type with a `+` operator and, sitting beside it, a stray public method called `op_CheckedAddition` that no C# syntax seems to produce. The decompiler is not wrong about the bytes — that method really exists. It is wrong about what the bytes *mean*. This post walks the lowering and shows what [Glass.NET](https://delta1labs.com/products/glass) has to recognise to give the keyword back.

## The real question

There is one keyword, `checked`, and there are two methods. A `checked(a + b)` and an ordinary `a + b` read almost identically in source, yet one has to reach a method that throws on overflow and the other a method that wraps. How does a single lexical context at the call site pick between two physically distinct methods in metadata — and how does a decompiler run that backwards, from the method names and the call tokens, to the `operator checked +` declaration the author actually wrote?

## Both operators, side by side

Here is a value type that carries both forms of addition. The unchecked one wraps, the checked one traps:

```csharp
public readonly struct Amount
{
    public int Value { get; }
    public Amount(int value) => Value = value;

    public static Amount operator +(Amount a, Amount b)
        => new(unchecked(a.Value + b.Value));

    public static Amount operator checked +(Amount a, Amount b)
        => new(checked(a.Value + b.Value));
}
```

The language rule is that a `checked` operator may be declared only if its unchecked counterpart exists too — `operator checked +` requires `operator +`. The compiler needs a method to call in an unchecked context, which is the default, so the unchecked form is the mandatory one and the checked form is the optional add-on.

## What the compiler emits

Both operators become static methods with the `specialname` flag. The names are where the whole scheme lives: the regular one keeps the reserved name `op_Addition`, and the checked one gets `Checked` spliced in after `op_`, giving `op_CheckedAddition`. Their bodies differ by exactly one instruction.

```
.method public hidebysig specialname static
        valuetype Amount op_Addition(valuetype Amount a, valuetype Amount b) cil managed
{
  ldarga.s a
  call     instance int32 Amount::get_Value()
  ldarga.s b
  call     instance int32 Amount::get_Value()
  add                      // wraps on overflow
  newobj   instance void Amount::.ctor(int32)
  ret
}

.method public hidebysig specialname static
        valuetype Amount op_CheckedAddition(valuetype Amount a, valuetype Amount b) cil managed
{
  ldarga.s a
  call     instance int32 Amount::get_Value()
  ldarga.s b
  call     instance int32 Amount::get_Value()
  add.ovf                  // the one instruction that differs
  newobj   instance void Amount::.ctor(int32)
  ret
}
```

The `add` versus `add.ovf` inside the bodies is an implementation detail of these two methods — it is *not* what a caller selects on. A caller never names an instruction; it names a method. So the decision a `checked` context drives is the choice of `op_Addition` or `op_CheckedAddition` at the `call`, not the choice of opcode inside either body.

The full family follows the same rule. The checked forms a type may declare are `op_CheckedAddition`, `op_CheckedSubtraction`, `op_CheckedMultiply`, `op_CheckedDivision`, `op_CheckedUnaryNegation`, `op_CheckedIncrement`, `op_CheckedDecrement`, and `op_CheckedExplicit` for conversions. Each one is the reserved name of its unchecked twin with `Checked` inserted after `op_`.

## The call site chooses

Which method the compiler calls is decided entirely by the lexical context around the use — the same `checked`/`unchecked` rule that governs built-in arithmetic:

```csharp
var wrap = a + b;              // unchecked (the default) → op_Addition
var trap = checked(a + b);     // checked expression       → op_CheckedAddition
checked { var t2 = a + b; }    // checked block            → op_CheckedAddition
```

```
call  valuetype Amount Amount::op_Addition(valuetype Amount, valuetype Amount)          // wrap
call  valuetype Amount Amount::op_CheckedAddition(valuetype Amount, valuetype Amount)   // trap
call  valuetype Amount Amount::op_CheckedAddition(valuetype Amount, valuetype Amount)   // t2
```

A whole-project `<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>` (the `/checked` compiler option) flips the default context to checked for every expression that is not inside an explicit `unchecked`, so a bare `a + b` then routes to `op_CheckedAddition` with no keyword in the source. And the fallback matters: if `Amount` declared only `operator +`, every `checked(a + b)` would still compile — the compiler calls `op_Addition` because there is nothing checked to call. The checked method is reached only when it exists *and* the context asks for it.

<figure class="my-8"><svg viewBox="0 0 760 300" role="img" aria-label="A type declaring operator plus and operator checked plus lowers to two specialname methods, op_Addition and op_CheckedAddition. A checked block at the call site emits a call token to op_CheckedAddition, and Glass.NET reads that name back into the operator checked plus declaration." xmlns="http://www.w3.org/2000/svg" style="max-width:760px;width:100%;height:auto"><defs><marker id="ckop" 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="16" y="26" width="214" height="110" rx="10" fill="none" stroke="#6366f1" stroke-width="1.5"/><text x="30" y="50" fill="#6366f1" font-family="sans-serif" font-size="13" font-weight="700">1 · Source</text><text x="30" y="74" fill="#64748b" font-family="monospace" font-size="12">operator +</text><text x="30" y="94" fill="#64748b" font-family="monospace" font-size="12">operator checked +</text><text x="30" y="118" fill="#64748b" font-family="sans-serif" font-size="11">two declarations</text><rect x="276" y="26" width="214" height="110" rx="10" fill="none" stroke="#0891b2" stroke-width="1.5"/><text x="290" y="50" fill="#0891b2" font-family="sans-serif" font-size="13" font-weight="700">2 · Metadata</text><text x="290" y="74" fill="#64748b" font-family="monospace" font-size="11.5">op_Addition</text><text x="290" y="94" fill="#64748b" font-family="monospace" font-size="11.5">op_CheckedAddition</text><text x="290" y="118" fill="#64748b" font-family="sans-serif" font-size="11">both specialname static</text><rect x="536" y="26" width="208" height="110" rx="10" fill="none" stroke="#0891b2" stroke-width="1.5"/><text x="550" y="50" fill="#0891b2" font-family="sans-serif" font-size="13" font-weight="700">3 · Call site</text><text x="550" y="74" fill="#64748b" font-family="monospace" font-size="12">checked { a + b }</text><text x="550" y="96" fill="#64748b" font-family="monospace" font-size="11">call op_CheckedAddition</text><text x="550" y="118" fill="#64748b" font-family="sans-serif" font-size="11">context picks the method</text><line x1="230" y1="80" x2="272" y2="80" stroke="#6366f1" stroke-width="1.5" marker-end="url(#ckop)"/><line x1="490" y1="80" x2="532" y2="80" stroke="#6366f1" stroke-width="1.5" marker-end="url(#ckop)"/><rect x="170" y="200" width="420" height="82" rx="10" fill="none" stroke="#10b981" stroke-width="1.5"/><text x="188" y="226" fill="#10b981" font-family="sans-serif" font-size="13" font-weight="700">Glass.NET reconstructs</text><text x="188" y="250" fill="#64748b" font-family="monospace" font-size="13">operator checked +  ·  checked(a + b)</text><text x="188" y="270" fill="#64748b" font-family="sans-serif" font-size="11">op_Checked* name + specialname → the keyword, not a method</text><line x1="470" y1="136" x2="420" y2="198" stroke="#10b981" stroke-width="1.5" marker-end="url(#ckop)"/></svg><figcaption class="text-sm text-muted mt-2">One keyword, two methods: the checked context at the call site selects op_CheckedAddition, and Glass reads the name back into operator checked +.</figcaption></figure>

## Reading it in Glass

Glass treats `op_Checked*` as first-class operator names. When it sees a `specialname static` method whose name is one of the checked family, with the arity the operator requires, it renders the declaration with the keyword in place — `public static Amount operator checked +(Amount a, Amount b)` — and keeps it paired with the unchecked `operator +` it was required to accompany. At each `call` site, it resolves the method token: a call to `op_CheckedAddition` becomes `checked(a + b)`, and a run of consecutive statements all calling checked forms folds into a single `checked { }` block, which is the shape the author most likely wrote.

If you want to see the mechanism rather than the result, open the assembly and switch a method between the **C#** and **IL** views. Line up each `checked(...)` in the source against the `call` to an `op_Checked*` method in the IL, and note that the opcode *inside* those methods (`add.ovf`) never appears at the call site — the caller only ever names a method. That is the whole feature in one screen: the keyword is carried by which method is called, not by any instruction the caller emits.

## Conversions reuse one name

Checked conversions are the sharp edge. Every checked conversion operator, regardless of target type, compiles to the single reserved name `op_CheckedExplicit`:

```csharp
public static explicit operator checked int(Amount a)   => checked((int)a.Value);
public static explicit operator checked short(Amount a) => checked((short)a.Value);
```

```
.method public hidebysig specialname static int32 op_CheckedExplicit(valuetype Amount a) cil managed
.method public hidebysig specialname static int16 op_CheckedExplicit(valuetype Amount a) cil managed
```

Two methods, same name, same single parameter, different **return type**. The CLI keeps both because a method's signature includes its return type; C# cannot express two ordinary methods that differ only by return type — that is error CS0111. The only legal C# syntax is the conversion operator, where the target type is part of the declaration. So for `op_CheckedExplicit`, rendering it as an operator is not a readability choice; it is the only form that compiles. Glass disambiguates the two by the return type carried in each `call` token and emits `explicit operator checked int` and `explicit operator checked short` accordingly.

## When a naive decompiler breaks

A decompiler whose operator table predates C# 11 does not recognise any `op_Checked*` name. Two failure modes follow, and both compile, which is what makes them dangerous.

On the declaration side it emits the method literally: `public static Amount op_CheckedAddition(Amount a, Amount b)`. That compiles, and it is wrong — the type has silently lost its checked behaviour, because a recompiled `checked(a + b)` now has no checked operator to find and falls back to the wrapping `op_Addition`. On the use side, every `checked(a + b)` the author wrote comes back as `Amount.op_CheckedAddition(a, b)`, a method call standing where an expression stood. For the conversion case the naive output does not even compile: two `op_CheckedExplicit` methods side by side are a CS0111 duplicate, with no obvious cause because each method reads fine alone.

Keep one distinction straight: this is entirely separate from the built-in overflow opcodes. `checked((byte)n)` on a primitive is `conv.ovf.u1` — an instruction, not a method — and there is no `op_` anything involved. That instruction-level case, including the asymmetries where `checked` emits no special opcode at all, is covered in [how checked arithmetic decompiles](https://delta1labs.com/blog/glass-decompile-checked-arithmetic-overflow-conv-ovf/); the broader operator-reconstruction picture is in [how operator overloads decompile](https://delta1labs.com/blog/decompile-operator-overloads-checked-conversions/). This post is only the named-method half: the operators a type declares, not the opcodes the compiler emits for the built-ins.

## Takeaway

A `checked` user-defined operator is a naming convention plus a flag: `op_Checked*`, `specialname`, and the right arity, sitting beside its unchecked twin. The keyword the author wrote is not in either method body — it is recoverable only from *which* method each call site selected, which in turn was fixed by the lexical `checked` context at compile time. Glass.NET reads the name, the flag, the arity and the call token together, so it can hand back `operator checked +` and `checked(a + b)` instead of a stray `op_CheckedAddition`. Glass is a free .NET decompiler for inspecting code you have the right to inspect — your own assemblies, your dependencies, and third-party binaries you are licensed to analyse — and preserving the checked operator is the difference between source that reads right and source that runs right.

