What does `@` mean at the beginning of a line in C #?

Consider the following line:

readonly private string TARGET_BTN_IMG_URL = @"\\ad1-sunglim\Test\"; 

On this line, why @ need to bind?

+7
string c #
source share
3 answers

It denotes a literal string in which the "\" character does not indicate an escape sequence.

+13
source share

@ tells C # to treat this as a literal string literal string literal . For example:

 string s = "C:\Windows\Myfile.txt"; 

is a mistake because \W and \M are not valid escape sequences. You should write it like this:

 string s = "C:\\Windows\\Myfile.txt"; 

To make it more understandable, you can use a literal string that does not recognize \ as a special character. Consequently:

 string s = @"C:\Windows\Myfile.txt"; 

fine.


EDIT: MSDN provides the following examples:

 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"; 
+7
source share

because the string contains the escape sequence "\". to tell the compiler not to consider "\" as an escape sequence, you need to use "@".

+2
source share

All Articles