Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 707
Xaser Avatar asked Jul 11 '15 16:07

Xaser


People also ask

What is the main cause of hep C?

Hepatitis C is a liver infection caused by the hepatitis C virus (HCV). Hepatitis C is spread through contact with blood from an infected person. Today, most people become infected with the hepatitis C virus by sharing needles or other equipment used to prepare and inject drugs.

Does hep C go away?

Hepatitis C virus (HCV) causes both acute and chronic infection. Acute HCV infections are usually asymptomatic and most do not lead to a life-threatening disease. Around 30% (15–45%) of infected persons spontaneously clear the virus within 6 months of infection without any treatment.

What does hep C pain feel like?

Many people with chronic HCV suffer from aches and pains in their joints. A variety of different joints can be involved but the most common are in the hands and wrists. These pains are often minor but occasionally the pain can be quite severe. In such cases painkillers can be used to relieve the symptoms.

How easy is it to get hep C?

Hepatitis C is spread only through exposure to an infected person's blood. High-risk activities include: Sharing drug use equipment. Anything involved with injecting street drugs, from syringes, to needles, to tourniquets, can have small amounts of blood on it that can transmit hepatitis C.


2 Answers

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 });
like image 71
i3arnon Avatar answered Oct 01 '22 09:10

i3arnon


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.

like image 25
brazilianldsjaguar Avatar answered Oct 01 '22 09:10

brazilianldsjaguar