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

前端 未结 6 1068
轻奢々
轻奢々 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条回答
  • 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);
    
    0 讨论(0)
  • 2020-11-22 10:22

    This is covered in section 2.4.4.5 of the C# specification:

    2.4.4.5 String literals

    C# supports two forms of string literals: regular string literals and verbatim string literals.

    A regular string literal consists of zero or more characters enclosed in double quotes, as in "hello", and may include both simple escape sequences (such as \t for the tab character) and hexadecimal and Unicode escape sequences.

    A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines.

    In other words the only special character in a @"verbatim string literal" is the double-quote character. If you wish to write a verbatim string containing a double-quote you must write two double-quotes. All other characters are interpreted literally.

    You can even have literal new lines in a verbatim string literal. In a regular string literal you cannot have literal new lines. Instead you must use for example "\n".

    Verbatim strings literals are often useful for embedding filenames and regular expressions in the source code, because backslashes in these types of strings are common and would need to be escaped if a regular string literal were used.

    There is no difference at runtime between strings created from regular string literals and strings created from a verbatim string literals - they are both of type System.String.

    0 讨论(0)
  • 2020-11-22 10:28

    If you want to suppress the ReSharper warnings, you can use:

    Localizable(false)
    

    For things like parameters, file locations, etc., this could be a good solution.

    0 讨论(0)
  • 2020-11-22 10:34

    A verbatim string is one that does not need to be escaped, like a filename:

    string myFileName = "C:\\myfolder\\myfile.txt";
    

    would be

    string myFileName = @"C:\myfolder\myfile.txt";
    

    The @ symbol means to read that string literally, and don't interpret control characters otherwise.

    0 讨论(0)
  • 2020-11-22 10:39

    You can have multiline string too using verbatim strings:

    Console.WriteLine(@"This
        is
        a
        Test
        for stackoverflow");
    

    without @ you got an error.

    In VB14 there is a new feature called Multiline Strings, it's like verbatim strings in C#.

    Multiline Strings

    Pro tip: VB string literals are now exactly like C# verbatim strings.

    0 讨论(0)
  • 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\\.");
    
    0 讨论(0)
提交回复
热议问题