新聞中心
本文轉(zhuǎn)載自微信公眾號(hào)「UP技術(shù)控」,作者conan5566。轉(zhuǎn)載本文請(qǐng)聯(lián)系UP技術(shù)控公眾號(hào)。

在湖口等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需搭建網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,成都營(yíng)銷網(wǎng)站建設(shè),外貿(mào)網(wǎng)站建設(shè),湖口網(wǎng)站建設(shè)費(fèi)用合理。
概述
索引器(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ù)可以是不同的類型。沒有必要讓索引器必須是整型的。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
當(dāng)前名稱:一篇文章帶你了解C#索引器
標(biāo)題URL:http://www.dlmjj.cn/article/cdcgcog.html


咨詢
建站咨詢
