一.忽略轉義字符
C# 中字符串常量可以以@ 開頭聲名,這樣的優點是轉義序列“不”被處理,按“原樣”輸出。
string s_FilePath ="C:\\Program Files\\Microsoft.NET\\test.txt";
相當於
string s_FilePath =@"C:\Program Files\Microsoft.NET\test.txt";
2.如要在一個用 @ 引起來的字符串中包括一個雙引號,就需要使用兩對雙引號了。這時候你不能使用 \ 來轉義雙引號了,因爲在這裏 \ 的轉義用途已經被 @ “屏蔽”掉了。
@"""Ahoy!"" cried the captain." // 輸出爲: "Ahoy!" cried the captain.
二.讓字符串跨行
例如
string strSQL = "SELECT * FROM HumanResources.Employee AS e"
" INNER JOIN Person.Contact AS c"
" ON e.ContactID = c.ContactID"
" ORDER BY c.LastName";
使用@後
string strSQL = @"SELECT * FROM HumanResources.Employee AS e
INNER JOIN Person.Contact AS c
ON e.ContactID = c.ContactID
ORDER BY c.LastName";
三.在標識符中的用法
C#是不允許關鍵字作爲標識符(類名、變量名、方法名、表空間名等)使用的,但如果加上@之後就可以了
例如
public static void @static(int @int)
3.在標識符中的用法
C#是不允許關鍵字作爲標識符(類名、變量名、方法名、表空間名等)使用的,但如果加上@之後就可以了
例如
public static void @static(int @int)
{
if (@int > 0)
{
System.Console.WriteLine("Positive Integer");
}
}