標籤:
1.限定字串
用 @ 符號加在字串前面表示其中的逸出字元“不”被處理。
如果我們寫一個檔案的路徑,例如"D:/文字檔"路徑下的text.txt檔案,不加@符號的話寫法如下:
string fileName = "D://文字檔//text.txt";
如果使用@符號就會比較簡單:
string fileName = @"D:/文字檔/text.txt";
2.讓字串跨行
有時候一個字串寫在一行中會很長(比如SQL語句),不使用@符號,一種寫法是這樣的:
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";
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");
}
}
}
}
C# @符號的多種使用方法