我可以做这个:
var log = string.Format("URL: {0}", url);
Run Code Online (Sandbox Code Playgroud)
或者甚至喜欢这个
var format = "URL: {0}";
...
var log = string.Format(format, url);
Run Code Online (Sandbox Code Playgroud)
我在format
其他地方定义并使用format
变量,而不是内联字符串.
在C#6中,这似乎是不可能的:
var format = $"URL: {url}"; // Error url does not exist
...
var url = "http://google.com";
...
var log = $format; // The way to evaluate string interpolation here
Run Code Online (Sandbox Code Playgroud)
无论如何使用字符串插值与先前声明的变量?
C#6似乎在编译期间内联字符串内联.但是,请考虑使用此功能进行本地化,在config中定义格式或仅const
在类中使用格式.