1.限定字串
用 @ 符號加在字串前面表示其中的逸出字元“不”被處理。
如果我們寫一個檔案的路徑,例如"D:/文字檔"路徑下的text.txt檔案,不加@符號的話寫法如下:
stringfileName="D://文字檔//text.txt";
如果使用@符號就會比較簡單:
stringfileName=@"D:/文字檔/text.txt";
2.讓字串跨行
有時候一個字串寫在一行中會很長(比如SQL語句),不使用@符號,一種寫法是這樣的:
複製代碼 代碼如下:string strSQL="SELECT * FROM HumanResources.Employee AS e"
+"INNER JOINPerson.Contact AS c" 3.+"ON e.ContactID=c.ContactID" 4.+"ORDERBY c.LastName";
加上@符號後就可以直接換行了: 複製代碼 代碼如下:string strSQL=@"SELECT * FROM HumanResources.Employee AS e INNER JOIN Person.Contact AS c ON e.ContactID=c.ContactID ORDERBYc.LastName";
3.在標識符中的用法
C#是不允許關鍵字作為標識符(類名、變數名、方法名、資料表空間名等)使用的,但如果加上@之後就可以了,例如: 複製代碼 代碼如下:namespace @namespace
{
class @class 4. {
public static void @static(int @int) 6. {
if (@int > 0) 8. {
System.Console.WriteLine("Positive Integer"); 10. }
else if (@int == 0) 12. {
System.Console.WriteLine("Zero"); 14. }
else 16. {
System.Console.WriteLine("Negative Integer"); 18. }
}
}
}