考虑以下代码:
readonly private string TARGET_BTN_IMG_URL = @"\\ad1-sunglim\Test\";
在这行代码中,为什么需要使用@符号?它表示一个字面字符串,在这个字符串中 '\' 字符不会被解释为转义字符。
TARGET_BTN_IMG_URL = "\\\\ad1-sunglim\\Test\\";
- Rubens Farias@ 告诉 C# 把它作为一个 逐字字符串字面值 来处理。例如:
string s = "C:\Windows\Myfile.txt";
这是一个错误,因为\W
和\M
不是有效的转义序列。你需要改成这样:
string s = "C:\\Windows\\Myfile.txt";
string s = @"C:\Windows\Myfile.txt";
完全没问题。
编辑:MSDN 提供了以下示例:
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";
"
,可以使用两个双引号""
来实现(例如上面的例子@"Joe said ""Hello"" to me"
)。 - Redwood因为你的字符串包含转义序列“\”,为了告诉编译器不要将“\”视为转义序列,你需要使用“@”。
x = @"abc";
- nickf