Menu

1 / 2

Decompiling C# string interpolation and interpolated string handlers

DEV Community: csharp·Zero Heartbeat·about 14 hours ago
#LoJWAB27
#devtocsharp#devto#dev#string#handler
Reading 0:00
15s threshold

An interpolated string is one of the most innocent-looking pieces of C# you can write — $"Hello {name}, you have {count} messages" — and one of the least predictable once it reaches IL. There is no interpolation instruction. The compiler rewrites every $"..." into ordinary method calls, and it has three completely different ways of doing so: a string.Concat , a string.Format with a positional template, or — on modern targets — a construct-and-append sequence against DefaultInterpolatedStringHandler . Which one it picks depends on your target framework, the types in the holes, and whether the receiving method opted into a handler of its own. Walking each lowering explains what a decompiler such as Glass.NET can fold back into $"..." , and where it can only show you the machinery the compiler actually emitted.

Three lowerings, one syntax

The single most useful fact about $"..." is that the syntax tells you almost nothing about the IL.…

Continue reading — create a free account

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