鍍金池/ 教程/ C#/ 索引器
循環(huán)
正則表達式
概述
委托
多態(tài)性
字符串
繼承
結構體
集合
變量
不安全代碼
判斷
反射
異常處理
可空類型
方法
數據類型
命名空間
文件 I/O
類型轉換
屬性
程序結構
事件
接口
預處理指令
運算符
多線程
匿名方法
索引器
泛型
封裝
常量和文字
基本語法
特性
數組
環(huán)境配置
運算符重載
枚舉

索引器

創(chuàng)建索引器可以使一個對象像數組一樣被索引。為類定義索引器時,該類的行為類似于一個虛擬數組,使用數組訪問運算符([ ])則可以對該類來進行訪問。

句法規(guī)則

創(chuàng)建一個一維索引器的規(guī)則如下:

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

   // set 訪問器
   set
   {
      // 設置 index 指定的值
   }
}

使用索引器

索引器的聲明在某種程度上類似于屬性的聲明,例如,使用 getset 方法來定義一個索引器。不同的是,屬性值的定義要求返回或設置一個特定的數據成員,而索引器的定義要求返回或設置的是某個對象實例的一個值,即索引器將實例數據切分成許多部分,然后通過一些方法去索引、獲取或是設置每個部分。

定義屬性需要提供屬性名,而定義索引器需要提供一個指向對象實例的 this 關鍵字。

示例:

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();
      }
   }
}

編譯執(zhí)行上述代碼,得到如下結果:

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

重載索引器

索引器允許重載,即允許使用多種不同類型的參數來聲明索引器。索引值可以是整數,但也可以是其他的數據類型,如字符型。

重載索引器示例:

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 參數的第一個索引器
         for (int i = 0; i < IndexedNames.size; i++)
         {
            Console.WriteLine(names[i]);
         }

         // 使用帶有 string 參數的第二個索引器
         Console.WriteLine(names["Nuha"]);
         Console.ReadKey();
      }
   }
}

編譯執(zhí)行上述代碼,得到如下結果:

Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.
2
上一篇:數組下一篇:字符串