课程表

C# 基础教程

C# 高级教程

C# 工具/手册

工具箱
速查手册

C# 索引器(Indexer)

当前位置:免费教程 » 程序设计 » C#

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

语法

一维索引器的语法如下:

  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),您可使用 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.  
  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. }

我来试一下

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

Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
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.  
  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. }

我来试一下

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

Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.
2
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号