c#中@標志的作用是什么

c#中@標志的作用是什么,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

創(chuàng)新互聯(lián)建站是一家專注于成都網(wǎng)站設計、網(wǎng)站制作和遂寧聯(lián)通機房的網(wǎng)絡公司,有著豐富的建站經(jīng)驗和案例。

1、在變量名前加@,可以告訴編譯器,@后的就是變量名。主要用于變量名和C#關(guān)鍵字重復時使用。

string[] @for = { "John", "James", "Joan", "Jamie" };for (int ctr = 0; ctr < @for.Length; ctr++){   Console.WriteLine($"Here is your gift, {@for[ctr]}!");}// The example displays the following output://     Here is your gift, John!//     Here is your gift, James!//     Here is your gift, Joan!//     Here is your gift, Jamie!

2、在字符串前加@,字符串中的轉(zhuǎn)義字符串將不再轉(zhuǎn)義。例外:""仍將轉(zhuǎn)義為",{{和}}仍將轉(zhuǎn)義為{和}。在同時使用字符串內(nèi)插和逐字字符串時,$要在@的前面

string filename1 = @"c:\documents\files\u0066.txt";string filename2 = "c:\\documents\\files\\u0066.txt";
Console.WriteLine(filename1);Console.WriteLine(filename2);// The example displays the following output://     c:\documents\files\u0066.txt//     c:\documents\files\u0066.txt

3、類似于第一條,用于在命名沖突時區(qū)分兩個特性名。特性Attribute自定義的類型名稱在起名時應以Attribute結(jié)尾,例如InfoAttribute,之后我們可以用InfoAttribute或Info來引用它。但是如果我們定義了兩個自定義特性,分別命名Info和InfoAttribute,則在使用Info這個名字時,編譯器就不知道是哪個了。這時,如果想用Info,就用@Info,想用InfoAttribute,就把名字寫全。

using System;
[AttributeUsage(AttributeTargets.Class)]public class Info : Attribute{   private string information;     public Info(string info)   {      information = info;   }}
[AttributeUsage(AttributeTargets.Method)]public class InfoAttribute : Attribute{   private string information;     public InfoAttribute(string info)   {      information = info;   }}
[Info("A simple executable.")] // Generates compiler error CS1614. Ambiguous Info and InfoAttribute. // Prepend '@' to select 'Info'. Specify the full name 'InfoAttribute' to select it.public class Example{   [InfoAttribute("The entry point.")]   public static void Main()   {   }}

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

新聞標題:c#中@標志的作用是什么
本文地址:http://muchs.cn/article18/ihggdp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設計、全網(wǎng)營銷推廣、用戶體驗、網(wǎng)站設計公司云服務器、網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設