开发者

What does it mean when I enclose a C# string in @" "? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

What does @ mean at the start of a string in C#?

Sorry but I can't find this on Google. I guess it maybe is not accepting my search string when I do a search.

Can someone tell me what this means in C#

开发者_Python百科var a = @"abc";

what's the meaning of the @?


It is a string literal. Which basically means it will take any character except ", including new lines. To write out a ", use "".


The advantage of @-quoting is that escape sequences are not processed, which makes it easy to write, for example, a fully qualified file name:

@"c:\Docs\Source\a.txt"  // rather than "c:\\Docs\\Source\\a.txt"


It means it's a literal string. Without it, any string containing a \ will consider the next character a special character, such as \n for new line. With a @ in front, it will treat the \ literally.

In the example you've given, there is no difference in the output.


This says that the characters inside the double quotation marks should be interpreted exactly as they are.

You can see that the backslash is treated as a character and not an escape sequence when the @ is used. The C# compiler also allows you to use real newlines in verbatim literals. You must encode quotation marks with double quotes.

string fileLocation = "C:\\CSharpProjects";
string fileLocation = @"C:\CSharpProjects";

Look at here for examples.


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.

Code Example

string a = "hello, world";                  // hello, world
string b = @"hello, world";               // hello, world
string c = "hello \t world";               // hello     world
string d = @"hello \t world";               // hello \t world
string e = "Joe said \"Hello\" to me";      // Joe said "Hello" to me
string f = @"Joe said ""Hello"" to me";   // Joe said "Hello" to me
string g = "\\\\server\\share\\file.txt";   // \\server\share\file.txt
string h = @"\\server\share\file.txt";      // \\server\share\file.txt
string i = "one\r\ntwo\r\nthree";
string j = @"one
two
three";

Reference link: MSDN

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜