Consider the following line:
readonly private string TARGET_BTN_IMG_URL = @"\\ad1-sunglim\Test\";
In this line, why does @ need to be attached?
Consider the following line:
readonly private string TARGET_BTN_IMG_URL = @"\\ad1-sunglim\Test\";
In this line, why does @ need to be attached?
It denotes a literal string, in which the '\' character does not indicate an escape sequence.
because you string contains escape sequence "\". in order to tell compiler not to treat "\" as escape sequence you have to use "@".
@ tells C# to treat that as a literal string verbatim string literal. For example:
string s = "C:\Windows\Myfile.txt";
is an error because \W
and \M
are not valid escape sequences. You need to write it this way instead:
string s = "C:\\Windows\\Myfile.txt";
To make it clearer, you can use a literal string, which does not recognize \ as a special character. Hence:
string s = @"C:\Windows\Myfile.txt";
is perfectly okay.
EDIT: MSDN provides these 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";