dotnet / dotnet/vblang

Interpolated strings should recognize constants

Open
#369 6 comments 5 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

Description

The usage of interpolated strings should recognize const strings.
The lines underneath should produce the same effect.

Private Const ASTRING As String = "FOO"

Console.WriteLine($"{ASTRING}BAR")
Console.WriteLine(ASTRING & "BAR")

Now the first line translates to an unnecessary string.format

IL_0000: ldstr "{0}BAR"
IL_0005: ldstr "FOO"
IL_000a: call string [mscorlib]System.String::Format(string, object)
IL_000f: call void [mscorlib]System.Console::WriteLine(string)

The second line does the concatenation at compile time

IL_0014: ldstr "FOOBAR"
IL_0019: call void [mscorlib]System.Console::WriteLine(string)

If the concatenation with constants is done at (pre)compile time before handling the interpolated string, then unneeded string format will automagically dissapear as this line does not produce a string format:

Console.WriteLine($"NOVARS")

results in this IL

IL_001e: ldstr "NOVARS"
IL_0023: call void [mscorlib]System.Console::WriteLine(string)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.