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"
+"ON e.ContactID=c.ContactID"
+"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
{
public static void @static(int @int)
{
if (@int > 0)
{
System.Console.WriteLine("Positive Integer");
}
else if (@int == 0)
{
System.Console.WriteLine("Zero");
}
else
{
System.Console.WriteLine("Negative Integer");
}
}
}
}