c#中@標(biāo)志的作用是什么,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)建站是一家專(zhuān)注于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作和遂寧聯(lián)通機(jī)房的網(wǎng)絡(luò)公司,有著豐富的建站經(jīng)驗(yàn)和案例。
1、在變量名前加@,可以告訴編譯器,@后的就是變量名。主要用于變量名和C#關(guān)鍵字重復(fù)時(shí)使用。
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)義為{和}。在同時(shí)使用字符串內(nèi)插和逐字字符串時(shí),$要在@的前面
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、類(lèi)似于第一條,用于在命名沖突時(shí)區(qū)分兩個(gè)特性名。特性Attribute自定義的類(lèi)型名稱(chēng)在起名時(shí)應(yīng)以Attribute結(jié)尾,例如InfoAttribute,之后我們可以用InfoAttribute或Info來(lái)引用它。但是如果我們定義了兩個(gè)自定義特性,分別命名Info和InfoAttribute,則在使用Info這個(gè)名字時(shí),編譯器就不知道是哪個(gè)了。這時(shí),如果想用Info,就用@Info,想用InfoAttribute,就把名字寫(xiě)全。
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)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
新聞標(biāo)題:c#中@標(biāo)志的作用是什么
本文地址:http://chinadenli.net/article18/ihggdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、全網(wǎng)營(yíng)銷(xiāo)推廣、用戶(hù)體驗(yàn)、網(wǎng)站設(shè)計(jì)公司、云服務(wù)器、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)