C# 索引器

C# 索引器(Indexer)

索引器(Indexer) 允许一个对象可以像数组一样被索引。当您为类定义一个索引器时,该类的行为就会像一个 虚拟数组(virtual array) 一样。您可以使用数组访问运算符([ ])来访问该类的实例。

语法

一维索引器的语法如下:

[csharp]  view plain  copy
  1. element-type this[int index]   
  2. {  
  3.    // get 访问器  
  4.    get   
  5.    {  
  6.       // 返回 index 指定的值  
  7.    }  
  8.   
  9.    // set 访问器  
  10.    set   
  11.    {  
  12.       // 设置 index 指定的值   
  13.    }  
  14. }  

索引器(Indexer)的用途

索引器的行为的声明在某种程度上类似于属性(property)。就像属性(property),您可使用 get 和 set 访问器来定义索引器。但是,属性返回或设置一个特定的数据成员,而索引器返回或设置对象实例的一个特定值。换句话说,它把实例数据分为更小的部分,并索引每个部分,获取或设置每个部分。

定义一个属性(property)包括提供属性名称。索引器定义的时候不带有名称,但带有 this 关键字,它指向对象实例。下面的实例演示了这个概念:

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

当上面的代码被编译和执行时,它会产生下列结果:

[csharp]  view plain  copy
  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)可被重载。索引器声明的时候也可带有多个参数,且每个参数可以是不同的类型。没有必要让索引器必须是整型的。C# 允许索引器可以是其他类型,例如,字符串类型。

下面的实例演示了重载索引器:

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

当上面的代码被编译和执行时,它会产生下列结果:

[csharp]  view plain  copy
  1. Zara  
  2. Riz  
  3. Nuha  
  4. Asif  
  5. Davinder  
  6. Sunil  
  7. Rubic  
  8. N. A.  
  9. N. A.  
  10. N. A.  
  11. 2  

猜你喜欢

转载自blog.csdn.net/weixin_39029925/article/details/78212389