我试图在插值字符串中使用条件运算符,但由于它中有冒号,编译器认为在冒号后出现格式字符串.
$"test {foo ? "foo is true" : "foo is false"}";
Run Code Online (Sandbox Code Playgroud)
我该如何使用这种声明?我想到的唯一一件事是这样的:
var fooString = foo ? "foo is true" : "foo is false";
$"test {fooString}";
Run Code Online (Sandbox Code Playgroud) CodeDomProvider objCodeCompiler = CodeDomProvider.CreateProvider( "CSharp" );
CompilerParameters objCompilerParameters = new CompilerParameters();
...
CompilerResults objCompileResults = objCodeCompiler.CompileAssemblyFromFile( objCompilerParameters, files.ToArray() );
Run Code Online (Sandbox Code Playgroud)
当我编译我的文件时,我得到:
FileFunctions.cs(347):错误:意外字符'$'
有没有人知道如何使用CodeDom编译进行字符串插值?
我找到了这个链接:如何使用CSharpCodeProvider定位.net 4.5?
所以我尝试过:
var providerOptions = new Dictionary<string, string>();
providerOptions.Add( "CompilerVersion", "v4.0" );
// Instantiate the compiler.
CodeDomProvider objCodeCompiler = CodeDomProvider.CreateProvider( "CSharp", providerOptions );
Run Code Online (Sandbox Code Playgroud)
但我仍然得到同样的错误.
我还将目标框架更新到.NET Framework 4.6.
注意:我不能指定"v4.5"或"v4.6"或者我会得到:
************** Exception Text **************
System.InvalidOperationException: Compiler executable file csc.exe cannot be found.
at System.CodeDom.Compiler.RedistVersionInfo.GetCompilerPath(IDictionary`2 provOptions, String compilerExecutable)
at Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch(CompilerParameters options, String[] fileNames)
at Microsoft.CSharp.CSharpCodeGenerator.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromFileBatch(CompilerParameters options, String[] fileNames)
at …Run Code Online (Sandbox Code Playgroud) Kotlin编译器是否"Hello, $name!"使用类似的东西进行翻译
java.lang.String.format("Hello, %s!", name)
Run Code Online (Sandbox Code Playgroud)
还是有其他机制?
如果我有一个这样的类,例如:
class Client {
val firstName: String
val lastName: String
val fullName: String
get() = "$firstName $lastName"
}
Run Code Online (Sandbox Code Playgroud)
这个getter会返回一个缓存的字符串,还是会尝试构建一个新的字符串?我应该使用lazyOf委托吗?
我知道除非会有数百万次调用fullName,否则不会出现性能问题,但除了如何使用之外,我还没有找到有关此功能的文档.
在Groovy中,我有一个定义的多行字符串,'''其中我需要使用插值来替换其他一些变量.
尽管我付出了很多努力,但我无法让它发挥作用 - 我认为我需要逃避一些我缺失的东西.
这是一些示例代码:
def cretanFood = "Dakos"
def mexicanFood = "Tacos"
def bestRestaurant = '''
${mexicanFood} & ${cretanFood}
'''
print bestRestaurant
Run Code Online (Sandbox Code Playgroud)
目前,这输出:
${mexicanFood} & ${cretanFood}
Run Code Online (Sandbox Code Playgroud)
虽然我很清楚地期望:
Tacos & Dakos
Run Code Online (Sandbox Code Playgroud)
(注意 - 我不想连接字符串)
MSDN 文档包含有关隐式转换的部分:
var s = $"hello, {name}";
System.IFormattable s = $"Hello, {name}";
System.FormattableString s = $"Hello, {name}";
Run Code Online (Sandbox Code Playgroud)
从第一个字符串开始,原始类型的插值字符串就是string.好的,我可以理解它,但是......我意识到字符串没有实现IFormattable.所以它看起来像编译器的一些魔法类似于它对lambdas的作用.
现在猜猜这段代码的输出:
void Main()
{
PrintMe("Hello World");
PrintMe($"{ "Hello World"}");
}
void PrintMe(object message)
{
Console.WriteLine("I am a " + message.GetType().FullName);
}
//void PrintMe(string message)
//{
// Console.WriteLine("I am a string " + message.GetType().FullName);
//}
void PrintMe(IFormattable message)
{
Console.WriteLine("I am a " + message.GetType().FullName);
}
Run Code Online (Sandbox Code Playgroud)
暗示:
我是System.String
我是System.Runtime.CompilerServices.FormattableStringFactory + ConcreteFormattableString
如果您从第二种方法中删除评论,您将获得:
我是一个字符串System.String
我是一个字符串System.String
好的
我可能不太了解重载分辨率,但是C#规范的 14.4.2 …
我总是使用以下语法来确保变量在字符串中展开:
"my string with a $($variable)"
我最近遇到了以下语法:
"my string with a ${variable}"
它们是等价的吗?有什么区别吗?
我在网上看过很多使用它的示例脚本.最近,我在一个关于TFS自动化的脚本中看到了它:
[string] $fields = "Title=$($taskTitle);Description=$($taskTitle);Assigned To=$($assignee);"
$fields += "Area Path=$($areaPath);Iteration Path=$($iterationPath);Discipline=$($taskDisciplineArray[$i]);Priority=$($i+1);"
$fields += "Estimate=$($taskEstimateArray[$i]);Remaining Work=$($taskRemainingArray[$i]);Completed Work=$($tasktaskCompletedArray[$i])"
Run Code Online (Sandbox Code Playgroud)
据我所知,$($taskTitle)似乎相当于$taskTitle.我错过了什么吗?有没有理由使用括号和额外的美元符号?
我刚刚在我的机器上安装了.NET Framework 4.6,然后使用Visual Studio 2013创建了一个针对.NET Framework 4.6的ConsoleApplication.
我在Main方法中写了以下内容:
string test = "Hello";
string format = $"{test} world!";
Run Code Online (Sandbox Code Playgroud)
但这不编译.在Visual Studio 2015中执行相同操作.
为什么?
插值字符串是C#6.0的新功能之一.
根据MSDN,嵌入式C#表达式的语法可以包含一个可选的逗号分隔值,如文档<optional-comma-field-width>中所述.
不幸的是,我没有找到这个字段的用途.
从名称可以看出,这个值设置了"插值"字段的最大大小,但是当我尝试以下表达式时:
var p = Process.GetCurrentProcess();
Console.WriteLine($"Process name is {p.ProcessName, 5}");
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
Process name is LINQPad.UserQuery
Run Code Online (Sandbox Code Playgroud) 为什么c#中的字符串插值不适用于const字符串?例如:
private const string WEB_API_ROOT = "/private/WebApi/";
private const string WEB_API_PROJECT = $"{WEB_API_ROOT}project.json";
Run Code Online (Sandbox Code Playgroud)
从我的角度来看,一切都在编译时就已知.或者这是一个稍后会添加的功能?
编译器消息:
分配给"DynamicWebApiBuilder.WEB_API_PROJECT"的表达式必须是常量.
非常感谢!