CallMeLaNN
CallMeLaNN

Reputation: 8588

String Interpolation with format variable

I can do this:

var log = string.Format("URL: {0}", url);

or even like this

var format = "URL: {0}";
...
var log = string.Format(format, url);

I have a format defined somewhere else and use the format variable, not inline string.

In C# 6, this is seems impossible:

var format = $"URL: {url}"; // Error url does not exist
...
var url = "http://google.com";
...
var log = $format; // The way to evaluate string interpolation here

Is there anyway to use string interpolation with variable declared earlier?

C# 6 seems interpolate the string inline during compile time. However consider using this feature for localization, define a format in config or simply having a format const in a class.

Upvotes: 38

Views: 43013

Answers (7)

Paulo Morgado
Paulo Morgado

Reputation: 14856

String interpolation is not library, but a compiler feature starting with C# 6.

The holes are not names, but expressions:

var r = new Rectangle(5, 4);
var s = $"Area: {r.Width * r.Height}":

How would you do that for localization, as you intend to?

Even r only exists at compile time. In IL it's just a position on the method's variable stack.

I've done what you intend to do for resources and configuration files.

Since you can only have a finite set of "variables" to substitute, what I did was have an array (or dictionary, if you prefer) and use a regular expression to replace the names in the holes with its index. What I did even allowed for format specifiers.

Upvotes: 5

Tod
Tod

Reputation: 2524

You can with the right Nuget package: https://www.nuget.org/packages/InterpolatedStringFormatter

var mystring = "a thing(and something {other})";
Console.WriteLine(mystring.Interpolate("else"));

Outputs:

a thing(and something else)

Upvotes: 1

Michael Schreiber
Michael Schreiber

Reputation: 43

This is supposed to be a comment to the answer from i3arnon but I do not have the reputation :-( : But for those who come to this old thread, in string.Format the format can be a variable:

string name = "bar";
string format = "{0}";
string result = string.Format(format, name);

works.

Upvotes: 2

Dimskiy
Dimskiy

Reputation: 5301

More of an idea as opposed to an answer.

For the example shown in the question, you can do the following.

var format = "URL: ";
...
var url = "http://google.com";
...
var result= $"{format} {url}";

I have an actual project where I have to do something like this a lot:

var label = "Some Label";
var value = "SomeValue";

//both label & value are results of some logic

var result = $"{label}: {value}";

Upvotes: 0

svick
svick

Reputation: 245046

One approach to work around that would be to use a lambda containing the interpolated string. Something like:

Func<string, string> formatter = url => $"URL: {url}";
...
var googleUrl = "http://google.com";
...
var log = formatter(googleUrl);

In C# 7.0, you could use a local function instead of a lambda, to make the code slightly simpler and more efficient:

string formatter(string url) => $"URL: {url}";
...
var googleUrl = "http://google.com";
...
var log = formatter(googleUrl);

Upvotes: 35

i3arnon
i3arnon

Reputation: 116666

No, you can't use string interpolation with something other than a string literal as the compiler creates a "regular" format string even when you use string interpolation.

Because this:

string name = "bar";
string result = $"{name}";

is compiled into this:

string name = "bar";
string result = string.Format("{0}", name);

the string in runtime must be a "regular" format string and not the string interpolation equivalent.

You can use the plain old String.Format instead.

Upvotes: 49

ivan
ivan

Reputation: 64

It seems that you can do it like this:

var googleUrl = "http://google.com";
var url = $"URL: {googleUrl}";

System.Console.WriteLine(url);

You can check for more details in https://msdn.microsoft.com/en-us/library/dn961160.aspx

Upvotes: -6

Related Questions