What is the difference between a regular string and a verbatim string?

前端 未结 6 1122
轻奢々
轻奢々 2020-11-22 10:11

I have a trial version of Resharper and it always suggests that I switch regular strings to verbatim strings. What is the difference?

6条回答
  •  Happy的楠姐
    2020-11-22 10:41

    Regular strings use special escape sequences to translate to special characters.

    /*
    This string contains a newline
    and a tab    and an escaped backslash\
    */
    Console.WriteLine("This string contains a newline\nand a tab\tand an escaped backslash\\");
    

    Verbatim strings are interpreted as is, without translating any escape sequences:

    /* 
    This string displays as is. No newlines\n, tabs\t or backslash-escapes\\.
    */
    Console.WriteLine(@"This string displays as is. No newlines\n, tabs\t or backslash-escapes\\.");
    

提交回复
热议问题