C# 索引器

2018-09-27 16:31 更新

索引器

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

句法規(guī)則

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

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

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

使用索引器

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

定義屬性需要提供屬性名,而定義索引器需要提供一個(gè)指向?qū)ο髮?shí)例的 this 關(guān)鍵字。

示例:

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í)行上述代碼,得到如下結(jié)果:

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

重載索引器

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

重載索引器示例:

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

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

Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.
2
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)