C# 索引器

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

一、语法

一维索引器的语法如下:

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

二、索引器(Indexer)的用途

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

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

  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. if( index >= 0 && index <= size-1 )
  19. {
  20. tmp = namelist[index];
  21. }
  22. else
  23. {
  24. tmp = "";
  25. }
  26. return ( tmp );
  27. }
  28. set
  29. {
  30. if( index >= 0 && index <= size-1 )
  31. {
  32. namelist[index] = value;
  33. }
  34. }
  35. }
  36. static void Main(string[] args)
  37. {
  38. IndexedNames names = new IndexedNames();
  39. names[0] = "Zara";
  40. names[1] = "Riz";
  41. names[2] = "Nuha";
  42. names[3] = "Asif";
  43. names[4] = "Davinder";
  44. names[5] = "Sunil";
  45. names[6] = "Rubic";
  46. for ( int i = 0; i < IndexedNames.size; i++ )
  47. {
  48. Console.WriteLine(names[i]);
  49. }
  50. Console.ReadKey();
  51. }
  52. }
  53. }

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

  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# 允许索引器可以是其他类型,例如,字符串类型。

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

  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. if( index >= 0 && index <= size-1 )
  21. {
  22. tmp = namelist[index];
  23. }
  24. else
  25. {
  26. tmp = "";
  27. }
  28. return ( tmp );
  29. }
  30. set
  31. {
  32. if( index >= 0 && index <= size-1 )
  33. {
  34. namelist[index] = value;
  35. }
  36. }
  37. }
  38. public int this[string name]
  39. {
  40. get
  41. {
  42. int index = 0;
  43. while(index < size)
  44. {
  45. if (namelist[index] == name)
  46. {
  47. return index;
  48. }
  49. index++;
  50. }
  51. return index;
  52. }
  53. }
  54. static void Main(string[] args)
  55. {
  56. IndexedNames names = new IndexedNames();
  57. names[0] = "Zara";
  58. names[1] = "Riz";
  59. names[2] = "Nuha";
  60. names[3] = "Asif";
  61. names[4] = "Davinder";
  62. names[5] = "Sunil";
  63. names[6] = "Rubic";
  64. // 使用带有 int 参数的第一个索引器
  65. for (int i = 0; i < IndexedNames.size; i++)
  66. {
  67. Console.WriteLine(names[i]);
  68. }
  69. // 使用带有 string 参数的第二个索引器
  70. Console.WriteLine(names["Nuha"]);
  71. Console.ReadKey();
  72. }
  73. }
  74. }

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

  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

🔚