Menu

Recovering checked user-defined operators
📰

Recovering checked user-defined operators

DEV Community: csharp·Zero Heartbeat·about 9 hours ago
#HcrzJYvN
#devtocsharp#devto#dev#checked#amount
Long article (1673 words) — no AI summary yet.
Reading 0:00
15s threshold

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:

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

Enter fullscreen mode

Exit fullscreen mode

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 has to recognise to give the keyword back.

The real question

There is one keyword, checked , and there are two methods.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.