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

前端 未结 6 1148
轻奢々
轻奢々 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 10:21

    There is no runtime difference between a string and verbatim string. They're only different at compile time. The compiler accepts fewer escape sequences in a verbatim string so what-you-see-is-what-you-get other than a quote escape.

    You can also use the verbatim character, @, to tell the compiler to treat a keyword as a name:

    var @if = "if";
    //okay, treated as a name
    Console.WriteLine(@if);
    //compiler err, if without @ is a keyword
    Console.WriteLine(if);
    
    var @a = "a";
    //okay
    Console.WriteLine(@a);
    //also okay, @ isn't part of the name
    Console.WriteLine(a);
    

提交回复
热议问题