C#中 @ 的3种植用途

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”);
           
}
       
}

相关文章