多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

中國最全I(xiàn)T社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2

csharp教程

C# 索引器(Indexer)

閱讀 (2068)

C# 索引器(Indexer)

索引器(Indexer) 允許一個對象可以像數(shù)組一樣被索引。當(dāng)您為類定義一個索引器時,該類的行為就會像一個 虛擬數(shù)組(virtual array) 一樣。您可以使用數(shù)組訪問運算符([ ])來訪問該類的實例。

語法

一維索引器的語法如下:

element-type this[int index] 
{
   // get 訪問器
   get 
   {
      // 返回 index 指定的值
   }

   // set 訪問器
   set 
   {
      // 設(shè)置 index 指定的值 
   }
}

索引器(Indexer)的用途

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

定義一個屬性(property)包括提供屬性名稱。索引器定義的時候不帶有名稱,但帶有 this 關(guān)鍵字,它指向?qū)ο髮嵗O旅娴膶嵗菔玖诉@個概念:

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í)行時,它會產(chǎn)生下列結(jié)果:

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

重載索引器(Indexer)

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

下面的實例演示了重載索引器:

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ù)的第一個索引器          for (int i = 0; i < IndexedNames.size; i++)          {             Console.WriteLine(names[i]);          }          // 使用帶有 string 參數(shù)的第二個索引器          Console.WriteLine(names["Nuha"]);          Console.ReadKey();       }    } } 

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

Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.
2
關(guān)閉
程序員人生
主站蜘蛛池模板: 免费人成网ww44kk44 | 色综合久久久高清综合久久久 | 国产日产欧美精品一区二区三区 | 国产chinesehdxxxxtube | 欧美性黑人极品hd | 国产精品亚洲二区 | 2015日韩永久免费视频播放 | 国产精品久久久久久麻豆一区 | 女人笫一次一级毛片 | 天堂在线免费 | 成人亚州 | 欧美最新一区二区三区四区 | 亚洲欧美激情精品一区二区 | 伊人精品影院一本到欧美 | 欧美性猛交xxxx乱大交中文 | 免费精品美女久久久久久久久久 | 另类老妇性bbwbbw | 日本视频在线观看不卡高清免费 | 日韩欧美一中文字幕不卡 | 精品偷自拍另类在线观看 | 福利片免费一区二区三区 | a毛片a毛片a视频 | 精品国产福利在线观看网址2022 | 在线看逼 | 日本高新1区2区3区 日本国产亚洲 | 91久久精品国产亚洲 | a毛片免费看 | 日韩欧美色 | 致命坏男人漫画登录页面免费漫画第三话 | 一区二区三区四区无限乱码 | аⅴ资源中文在线天堂 | 亚洲性生活 | 国产尤物在线播放 | 婷婷在线视频国产综合 | 国产精品亚洲午夜一区二区三区 | 午夜羞羞影院 | 婷婷在线观看视频 | 男人边吃奶边摸下面好爽视频 | 国产婷婷一区二区在线观看 | 在线播放a 1 | 免费在线视频播放 |