C# quotes in interpolated string Unexpected character \0022

大憨熊 提交于 2019-12-10 20:27:38

问题


I've read that you can use expressions in interpolated strings, but escaping quotes does not work.

private string sth = $"{String.Join(\"\", Node.stringToType.Keys)}";


Error CS1056: Unexpected character `\0022' (CS1056)
Error CS1525: Unexpected symbol `)', expecting `${', `:', or `}' (CS1525) 

UPDATE:

The inner expression above was ment to be equivalent to

String.Join("", Node.stringToType.Keys)

(the two backslashes were for escaping the two double quotes) like that you can insert there any delimiter.


回答1:


Change it to this

private string sth = $"{String.Join("\\", Node.stringToType.Keys)}";

This way must work too

private string sth = $"{String.Join(@"\", Node.stringToType.Keys)}";



回答2:


You should put it as

  private string sth = $"{String.Join("\\", Node.stringToType.Keys)}";

please, notice that the text within {...} should be correct C# code

  String.Join("\\", Node.stringToType.Keys)

As a further improvement, you don't want string interpolation at all:

  private string sth = String.Join("\\", Node.stringToType.Keys);


来源:https://stackoverflow.com/questions/43077856/c-sharp-quotes-in-interpolated-string-unexpected-character-0022

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!