Does C# 6.0's String interpolation rely on Reflection?
Asked Answered
C

2

8

Short and simple. Does the new string interpolation in C# 6.0 rely on reflection? I.e. does

string myStr = $"Hi {name}, how are you?";

use reflection at runtime to find the variable "name" and its value?

Compatriot answered 11/7, 2015 at 16:35 Comment(4)
Why would it need to? The compiler has all the information it needs. If the variable name name is in scope, it will be used, otherwise you'll have an error.Oshinski
I don't know, that's why I asked. I wasn't sure if the format string was saved "as is" in the binary and would then fill in the blanks (in the classical case {0} etc with the variables). If that were the case then it would have to use reflection, but I don't know how these statements are handled by the compilerCompatriot
I don't know but for sure I would peek a decompiler such as dotPeek to see what is going onGertrudgertruda
You can reason it out, Reflection is incapable of detecting $ on a string literal. Only the compiler can see it. Which tells it to parse the string, look for {identifier} in the string and generate the appropriate String.Format() statement.Ultrasonic
H
19

No. It doesn't. It is completely based on compile-time evaluation.

You can see that with this TryRoslyn example that compiles and decompiles this:

int name = 4;
string myStr = $"Hi {name}, how are you?";

Into this:

int num = 4;
string.Format("Hi {0}, how are you?", num);

string interpolation also supports using IFormattable as the result so (again using TryRoslyn) this:

int name = 4;
IFormattable myStr = $"Hi {name}, how are you?";

Turns into this:

int num = 4;
FormattableStringFactory.Create("Hi {0}, how are you?", new object[] { num });
Hortenciahortensa answered 11/7, 2015 at 16:39 Comment(5)
Thanks! I was about to ask if it's completely equivalent to a String.Format but this TryRoslyn Example made everything clearer :) will accept asap.Compatriot
@i3arnon, I was surprized that $"Hi {0}, how are you?"; compiled and put 0 as argument to string.Format. It's obvious why compiler let you to put code inside: $"Hi {friend.GiveMeYourName()}, how are you?";. But reasoning behind ability to put numbers is very interesting.Mancy
@LeonidVasilyev why? It's just an expression. Why should the compiler act any differently? It would be an interesting line of code to write, but the compiler should let you write it.Hortenciahortensa
@Hortenciahortensa name vs num - typo?Leonoraleonore
@Leonoraleonore No. It's the result of the TryRoslyn tool. It's probably num for number as it's an int.Hortenciahortensa
O
3

This article explains that it's compile-time based (and internally calls string.Format(). A quote:

String interpolation is transformed at compile time to invoke an equivalent string.Format call. This leaves in place support for localization as before (though still with composite format strings) and doesn’t introduce any post compile injection of code via strings.

Orelle answered 11/7, 2015 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.