轉自:http://dev.firnow.com/course/3_program/vc/vc_js/20090803/168333.html

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

arrow
arrow
    全站熱搜

    mming 發表在 痞客邦 留言(1) 人氣()