C#索引器有什么作用

這篇文章主要介紹“C# 索引器有什么作用”,在日常操作中,相信很多人在C# 索引器有什么作用問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”C# 索引器有什么作用”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

成都創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司一直秉承“誠(chéng)信做人,踏實(shí)做事”的原則,不欺瞞客戶,是我們最起碼的底線! 以服務(wù)為基礎(chǔ),以質(zhì)量求生存,以技術(shù)求發(fā)展,成交一個(gè)客戶多一個(gè)朋友!專注中小微企業(yè)官網(wǎng)定制,網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作,塑造企業(yè)網(wǎng)絡(luò)形象打造互聯(lián)網(wǎng)企業(yè)效應(yīng)。

概述

索引器(Indexer) 允許一個(gè)對(duì)象可以像數(shù)組一樣使用下標(biāo)的方式來(lái)訪問(wèn)。

當(dāng)您為類定義一個(gè)索引器時(shí),該類的行為就會(huì)像一個(gè) 虛擬數(shù)組(virtual array) 一樣。您可以使用數(shù)組訪問(wèn)運(yùn)算符 [ ]  來(lái)訪問(wèn)該類的的成員。

語(yǔ)法

一維索引器的語(yǔ)法如下:

element-type this[int index] {    // get 訪問(wèn)器    get    {       // 返回 index 指定的值    }     // set 訪問(wèn)器    set    {       // 設(shè)置 index 指定的值    } }

索引器(Indexer)的用途

索引器的行為的聲明在某種程度上類似于屬性(property)。就像屬性(property),您可使用 get 和 set  訪問(wèn)器來(lái)定義索引器。但是,屬性返回或設(shè)置一個(gè)特定的數(shù)據(jù)成員,而索引器返回或設(shè)置對(duì)象實(shí)例的一個(gè)特定值。換句話說(shuō),它把實(shí)例數(shù)據(jù)分為更小的部分,并索引每個(gè)部分,獲取或設(shè)置每個(gè)部分。

定義一個(gè)屬性(property)包括提供屬性名稱。索引器定義的時(shí)候不帶有名稱,但帶有 this 關(guān)鍵字,它指向?qū)ο髮?shí)例。下面的實(shí)例演示了這個(gè)概念:

using System; namespace IndexerApplication {    class IndexedNames    {       private string[] namelist = new string[size];       static public int size = 10;       public IndexedNames()       {          for (int i = 0; i < size; i++)          namelist[i] = "N. A.";       }       public string this[int index]       {          get          {             string tmp;              if( index >= 0 && index <= size-1 )             {                tmp = namelist[index];             }             else             {                tmp = "";             }              return ( tmp );          }          set          {             if( index >= 0 && index <= size-1 )             {                namelist[index] = value;             }          }       }        static void Main(string[] args)       {          IndexedNames names = new IndexedNames();          names[0] = "Zara";          names[1] = "Riz";          names[2] = "Nuha";          names[3] = "Asif";          names[4] = "Davinder";          names[5] = "Sunil";          names[6] = "Rubic";          for ( int i = 0; i < IndexedNames.size; i++ )          {             Console.WriteLine(names[i]);          }          Console.ReadKey();       }    } }

當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:

Zara Riz Nuha Asif Davinder Sunil Rubic N. A. N. A. N. A.

重載索引器(Indexer)

索引器(Indexer)可被重載。索引器聲明的時(shí)候也可帶有多個(gè)參數(shù),且每個(gè)參數(shù)可以是不同的類型。沒(méi)有必要讓索引器必須是整型的。C#  允許索引器可以是其他類型,例如,字符串類型。

下面的實(shí)例演示了重載索引器:

using System; namespace IndexerApplication {    class IndexedNames    {       private string[] namelist = new string[size];       static public int size = 10;       public IndexedNames()       {          for (int i = 0; i < size; i++)          {           namelist[i] = "N. A.";          }       }       public string this[int index]       {          get          {             string tmp;              if( index >= 0 && index <= size-1 )             {                tmp = namelist[index];             }             else             {                tmp = "";             }              return ( tmp );          }          set          {             if( index >= 0 && index <= size-1 )             {                namelist[index] = value;             }          }       }       public int this[string name]       {          get          {             int index = 0;             while(index < size)             {                if (namelist[index] == name)                {                 return index;                }                index++;             }             return index;          }        }        static void Main(string[] args)       {          IndexedNames names = new IndexedNames();          names[0] = "Zara";          names[1] = "Riz";          names[2] = "Nuha";          names[3] = "Asif";          names[4] = "Davinder";          names[5] = "Sunil";          names[6] = "Rubic";          // 使用帶有 int 參數(shù)的第一個(gè)索引器          for (int i = 0; i < IndexedNames.size; i++)          {             Console.WriteLine(names[i]);          }          // 使用帶有 string 參數(shù)的第二個(gè)索引器          Console.WriteLine(names["Nuha"]);          Console.ReadKey();       }    } }

當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:

Zara Riz Nuha Asif Davinder Sunil Rubic N. A. N. A. N. A. 2

到此,關(guān)于“C# 索引器有什么作用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

網(wǎng)站題目:C#索引器有什么作用
標(biāo)題路徑:http://muchs.cn/article48/ihpoep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站建設(shè)、網(wǎng)站制作、企業(yè)網(wǎng)站制作、做網(wǎng)站、云服務(wù)器

廣告

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

外貿(mào)網(wǎng)站制作