What's the @ in front of a string in C#?

前端 未结 9 1420
一整个雨季
一整个雨季 2020-11-22 01:04

This is a .NET question for C# (or possibly VB.net), but I am trying to figure out what\'s the difference between the following declarations:

string hello =          


        
相关标签:
9条回答
  • 2020-11-22 01:52

    The explanation is simple. To represent the string "string\", the compiler needs "string\\" because \ is an escape character. If you use @"string\" instead, you can forget about \\.

    0 讨论(0)
  • 2020-11-22 01:54

    Copied from MSDN:

    At compile time, verbatim strings are converted to ordinary strings with all the same escape sequences. Therefore, if you view a verbatim string in the debugger watch window, you will see the escape characters that were added by the compiler, not the verbatim version from your source code. For example, the verbatim string @"C:\files.txt" will appear in the watch window as "C:\\files.txt".

    0 讨论(0)
  • 2020-11-22 01:58

    http://msdn.microsoft.com/en-us/library/aa691090.aspx

    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.

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