C# @符號的多種使用方法

來源:互聯網
上載者:User

標籤:

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# @符號的多種使用方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.