How to use escape characters with string interpolation in C# 6?

后端 未结 7 542
孤街浪徒
孤街浪徒 2020-12-03 06:42

I\'ve been using string interpolation and loving it, however I have an issue where I am trying to include a backslash in my output, but am not able to get it to work.

相关标签:
7条回答
  • 2020-12-03 07:11

    Hi the rule for escape the back slash in interpolated string is duplicate the back slash:

    var domain = "mydomain";
    var userName = "myUserName";
    var combo = $"{domain}\\{userName}";
    

    but if you also use the interpolated string as verbatim string then you don't need to escape the back slash:

    var domain = "mydomain";
    var userName = "myUserName";
    var combo = $@"{domain}\{userName}";
    

    and you get the same:

    For a tutorial about interpolated string: see video interpolated string

    0 讨论(0)
提交回复
热议问题