天天看點

C#之@用法和using使用

==>@的用法

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

            }

        }

==>using的用法

Using除了能夠添加引用外,還可以實作對一個執行個體對象設定活動區域,當離開了這個活動區域,則就自動調用這個類執行個體的Dispose方法來銷毀這個對象,當然我們可以使用Try...Catch來實作,不過,也可以使用using關鍵字來實作。