日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
一篇文章帶你了解C#索引器

本文轉(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ǔ)法如下:

 
 
 
  1. element-type this[int index] 
  2.    // get 訪問(wèn)器 
  3.    get 
  4.    { 
  5.       // 返回 index 指定的值 
  6.    } 
  7.  
  8.    // set 訪問(wèn)器 
  9.    set 
  10.    { 
  11.       // 設(shè)置 index 指定的值 
  12.    } 

索引器(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è)概念:

 
 
 
  1. using System; 
  2. namespace IndexerApplication 
  3.    class IndexedNames 
  4.    { 
  5.       private string[] namelist = new string[size]; 
  6.       static public int size = 10; 
  7.       public IndexedNames() 
  8.       { 
  9.          for (int i = 0; i < size; i++) 
  10.          namelist[i] = "N. A."; 
  11.       } 
  12.       public string this[int index] 
  13.       { 
  14.          get 
  15.          { 
  16.             string tmp; 
  17.  
  18.             if( index >= 0 && index <= size-1 ) 
  19.             { 
  20.                tmp = namelist[index]; 
  21.             } 
  22.             else 
  23.             { 
  24.                tmp = ""; 
  25.             } 
  26.  
  27.             return ( tmp ); 
  28.          } 
  29.          set 
  30.          { 
  31.             if( index >= 0 && index <= size-1 ) 
  32.             { 
  33.                namelist[index] = value; 
  34.             } 
  35.          } 
  36.       } 
  37.  
  38.       static void Main(string[] args) 
  39.       { 
  40.          IndexedNames names = new IndexedNames(); 
  41.          names[0] = "Zara"; 
  42.          names[1] = "Riz"; 
  43.          names[2] = "Nuha"; 
  44.          names[3] = "Asif"; 
  45.          names[4] = "Davinder"; 
  46.          names[5] = "Sunil"; 
  47.          names[6] = "Rubic"; 
  48.          for ( int i = 0; i < IndexedNames.size; i++ ) 
  49.          { 
  50.             Console.WriteLine(names[i]); 
  51.          } 
  52.          Console.ReadKey(); 
  53.       } 
  54.    } 

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

 
 
 
  1. Zara 
  2. Riz 
  3. Nuha 
  4. Asif 
  5. Davinder 
  6. Sunil 
  7. Rubic 
  8. N. A. 
  9. N. A. 
  10. N. A. 

重載索引器(Indexer)

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

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

 
 
 
  1. using System; 
  2. namespace IndexerApplication 
  3.    class IndexedNames 
  4.    { 
  5.       private string[] namelist = new string[size]; 
  6.       static public int size = 10; 
  7.       public IndexedNames() 
  8.       { 
  9.          for (int i = 0; i < size; i++) 
  10.          { 
  11.           namelist[i] = "N. A."; 
  12.          } 
  13.       } 
  14.       public string this[int index] 
  15.       { 
  16.          get 
  17.          { 
  18.             string tmp; 
  19.  
  20.             if( index >= 0 && index <= size-1 ) 
  21.             { 
  22.                tmp = namelist[index]; 
  23.             } 
  24.             else 
  25.             { 
  26.                tmp = ""; 
  27.             } 
  28.  
  29.             return ( tmp ); 
  30.          } 
  31.          set 
  32.          { 
  33.             if( index >= 0 && index <= size-1 ) 
  34.             { 
  35.                namelist[index] = value; 
  36.             } 
  37.          } 
  38.       } 
  39.       public int this[string name] 
  40.       { 
  41.          get 
  42.          { 
  43.             int index = 0; 
  44.             while(index < size) 
  45.             { 
  46.                if (namelist[index] == name) 
  47.                { 
  48.                 return index; 
  49.                } 
  50.                index++; 
  51.             } 
  52.             return index; 
  53.          } 
  54.  
  55.       } 
  56.  
  57.       static void Main(string[] args) 
  58.       { 
  59.          IndexedNames names = new IndexedNames(); 
  60.          names[0] = "Zara"; 
  61.          names[1] = "Riz"; 
  62.          names[2] = "Nuha"; 
  63.          names[3] = "Asif"; 
  64.          names[4] = "Davinder"; 
  65.          names[5] = "Sunil"; 
  66.          names[6] = "Rubic"; 
  67.          // 使用帶有 int 參數(shù)的第一個(gè)索引器 
  68.          for (int i = 0; i < IndexedNames.size; i++) 
  69.          { 
  70.             Console.WriteLine(names[i]); 
  71.          } 
  72.          // 使用帶有 string 參數(shù)的第二個(gè)索引器 
  73.          Console.WriteLine(names["Nuha"]); 
  74.          Console.ReadKey(); 
  75.       } 
  76.    } 

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

 
 
 
  1. Zara 
  2. Riz 
  3. Nuha 
  4. Asif 
  5. Davinder 
  6. Sunil 
  7. Rubic 
  8. N. A. 
  9. N. A. 
  10. N. A. 

當(dāng)前名稱:一篇文章帶你了解C#索引器
標(biāo)題URL:http://www.dlmjj.cn/article/cdcgcog.html