一篇文章带你了解C# 索引器

开发 后端
索引器(Indexer) 允许一个对象可以像数组一样使用下标的方式来访问。当您为类定义一个索引器时,该类的行为就会像一个 虚拟数组(virtual array) 一样。您可以使用数组访问运算符 [ ] 来访问该类的的成员。

[[393209]]

本文转载自微信公众号「UP技术控」,作者conan5566。转载本文请联系UP技术控公众号。

概述

索引器(Indexer) 允许一个对象可以像数组一样使用下标的方式来访问。

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

语法

一维索引器的语法如下:

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

索引器(Indexer)的用途

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

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

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

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

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

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

  1. Zara 
  2. Riz 
  3. Nuha 
  4. Asif 
  5. Davinder 
  6. Sunil 
  7. Rubic 
  8. N. A. 
  9. N. A. 
  10. N. A. 

 

责任编辑:武晓燕 来源: UP技术控
相关推荐

2023-05-12 08:19:12

Netty程序框架

2021-06-30 00:20:12

Hangfire.NET平台

2023-09-06 14:57:46

JavaScript编程语言

2021-05-18 08:30:42

JavaScript 前端JavaScript时

2023-07-30 15:18:54

JavaScript属性

2021-01-26 23:46:32

JavaScript数据结构前端

2021-03-05 18:04:15

JavaScript循环代码

2024-01-30 13:47:45

2021-03-09 14:04:01

JavaScriptCookie数据

2021-06-24 09:05:08

JavaScript日期前端

2021-09-27 09:18:30

ListIterato接口方法

2020-12-08 08:09:49

SVG图标Web

2024-04-19 14:23:52

SwitchJavaScript开发

2021-02-26 20:01:57

SVG滤镜元素

2023-05-08 08:21:15

JavaNIO编程

2020-11-17 11:10:21

CSS选择器HTML

2021-01-29 18:41:16

JavaScript函数语法

2021-02-02 18:39:05

JavaScript

2021-06-04 09:56:01

JavaScript 前端switch

2020-11-10 10:48:10

JavaScript属性对象
点赞
收藏

51CTO技术栈公众号