C# - 索引器

索引器允许对对象(例如数组)进行索引。为类定义索引器后,该类的行为类似于虚拟数组。然后,您可以使用数组访问运算符 ([ ]) 访问该类的实例。

语法

一维索引器的语法如下:

element-type this[int index] {

    // get 访问器。
    get {
        // 返回索引指定的值
    }
    
    // set 访问器。
    set {
        // 设置索引指定的值
    }
}

索引器的使用

索引器的行为声明在某种程度上类似于属性的声明。与属性类似,您可以使用 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();
      }
   }
}

当编译并执行上述代码时,它会产生以下结果 -

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

重载索引器

索引器可以重载。索引器也可以使用多个参数声明,每个参数可以是不同的类型。索引不必是整数。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 参数的第一个索引器
         for (int i = 0; i < IndexedNames.size; i++) {
            Console.WriteLine(names[i]);
         }
         
         //使用带有字符串参数的第二个索引器
         Console.WriteLine(names["Nuha"]);
         Console.ReadKey();
      }
   }
}

当编译并执行上述代码时,它会产生以下结果 -

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