String Interpolation

Rumbu rumbu at rumbu.ro
Mon Oct 23 14:12:25 UTC 2023


On Saturday, 21 October 2023 at 18:59:13 UTC, Adam D Ruppe wrote:
> On Saturday, 21 October 2023 at 18:31:10 UTC, Imperatorn wrote:
>> In C# an interpolated string is just a string
>
> This isn't true. It is an object that can convert to string 
> (similar to D's `alias toString this;`)

In fact neither, it's syntactic sugar.

```csharp
int count = 10;
string s = $"I drink {count} beers";
```

is syntactic sugar for:

```csharp
int count = 10;
DefaultInterpolationHandler dh = new 
DefaultInterpolationHandler();
dh.AppendLiteral("I drink ");
dh.AppendFormatted(count);
dh.AppendLiteral(" beers");
string s = dh.ToStringAndClear();
```

 From compiler point of view, Roslyn stores in AST a interpolated 
string as an array of expressions which get converted before IL 
compilation to the code above.





More information about the Digitalmars-d mailing list