C#中@的作用
@的作用
1,忽略轉義字符
例如
string fileName = "D:\\文本文件\\text.txt";
使用@後
string fileName = @"D:\文本文件\text.txt";
2,讓字符串跨行輸出
例如:
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#是不允許關鍵字作爲標識符(類名、變量名、方法名、表空間名等)使用的,但如果加上@
之後就可以了
例如:
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");
}