C# 索引器
索引器(Indexer) 允许一个对象可以像数组一样被索引。当您为类定义一个索引器时,该类的行为就会像一个 虚拟数组(virtual array) 一样。您可以使用数组访问运算符([ ])来访问该类的实例。
一、语法
一维索引器的语法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
element-type <span class="hljs-keyword">this</span>[<span class="hljs-keyword">int</span> index] { <span class="hljs-comment">// get 访问器</span> <span class="hljs-keyword">get</span> { <span class="hljs-comment">// 返回 index 指定的值</span> } <span class="hljs-comment">// set 访问器</span> <span class="hljs-keyword">set</span> { <span class="hljs-comment">// 设置 index 指定的值 </span> } } |
二、索引器(Indexer)的用途
索引器的行为的声明在某种程度上类似于属性(property)。就像属性(property),您可使用 get 和 set 访问器来定义索引器。但是,属性返回或设置一个特定的数据成员,而索引器返回或设置对象实例的一个特定值。换句话说,它把实例数据分为更小的部分,并索引每个部分,获取或设置每个部分。
定义一个属性(property)包括提供属性名称。索引器定义的时候不带有名称,但带有 this 关键字,它指向对象实例。下面的实例演示了这个概念:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<span class="hljs-keyword">using</span> System; <span class="hljs-keyword">namespace</span> <span class="hljs-title">IndexerApplication</span> { <span class="hljs-keyword">class</span> <span class="hljs-title">IndexedNames</span> { <span class="hljs-keyword">private</span> <span class="hljs-keyword">string</span>[] namelist = <span class="hljs-keyword">new</span> <span class="hljs-keyword">string</span>[size]; <span class="hljs-keyword">static</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> size = <span class="hljs-number">10</span>; <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">IndexedNames</span>() </span>{ <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < size; i++) namelist[i] = <span class="hljs-string">"N. A."</span>; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> <span class="hljs-keyword">this</span>[<span class="hljs-keyword">int</span> index] { <span class="hljs-keyword">get</span> { <span class="hljs-keyword">string</span> tmp; <span class="hljs-keyword">if</span>( index >= <span class="hljs-number">0</span> && index <= size<span class="hljs-number">-1</span> ) { tmp = namelist[index]; } <span class="hljs-keyword">else</span> { tmp = <span class="hljs-string">""</span>; } <span class="hljs-keyword">return</span> ( tmp ); } <span class="hljs-keyword">set</span> { <span class="hljs-keyword">if</span>( index >= <span class="hljs-number">0</span> && index <= size<span class="hljs-number">-1</span> ) { namelist[index] = <span class="hljs-keyword">value</span>; } } } <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"><span class="hljs-keyword">string</span>[] args</span>) </span>{ IndexedNames names = <span class="hljs-keyword">new</span> IndexedNames(); names[<span class="hljs-number">0</span>] = <span class="hljs-string">"Zara"</span>; names[<span class="hljs-number">1</span>] = <span class="hljs-string">"Riz"</span>; names[<span class="hljs-number">2</span>] = <span class="hljs-string">"Nuha"</span>; names[<span class="hljs-number">3</span>] = <span class="hljs-string">"Asif"</span>; names[<span class="hljs-number">4</span>] = <span class="hljs-string">"Davinder"</span>; names[<span class="hljs-number">5</span>] = <span class="hljs-string">"Sunil"</span>; names[<span class="hljs-number">6</span>] = <span class="hljs-string">"Rubic"</span>; <span class="hljs-keyword">for</span> ( <span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < IndexedNames.size; i++ ) { Console.WriteLine(names[i]); } Console.ReadKey(); } } } |
当上面的代码被编译和执行时,它会产生下列结果:
1 2 3 4 5 6 7 8 9 10 11 |
Zara Riz Nuha Asif Davinder Sunil Rubic N. A. N. A. N. A. |
三、重载索引器(Indexer)
索引器(Indexer)可被重载。索引器声明的时候也可带有多个参数,且每个参数可以是不同的类型。没有必要让索引器必须是整型的。C# 允许索引器可以是其他类型,例如,字符串类型。
下面的实例演示了重载索引器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
<span class="hljs-keyword">using</span> System; <span class="hljs-keyword">namespace</span> <span class="hljs-title">IndexerApplication</span> { <span class="hljs-keyword">class</span> <span class="hljs-title">IndexedNames</span> { <span class="hljs-keyword">private</span> <span class="hljs-keyword">string</span>[] namelist = <span class="hljs-keyword">new</span> <span class="hljs-keyword">string</span>[size]; <span class="hljs-keyword">static</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> size = <span class="hljs-number">10</span>; <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">IndexedNames</span>() </span>{ <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < size; i++) { namelist[i] = <span class="hljs-string">"N. A."</span>; } } <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> <span class="hljs-keyword">this</span>[<span class="hljs-keyword">int</span> index] { <span class="hljs-keyword">get</span> { <span class="hljs-keyword">string</span> tmp; <span class="hljs-keyword">if</span>( index >= <span class="hljs-number">0</span> && index <= size<span class="hljs-number">-1</span> ) { tmp = namelist[index]; } <span class="hljs-keyword">else</span> { tmp = <span class="hljs-string">""</span>; } <span class="hljs-keyword">return</span> ( tmp ); } <span class="hljs-keyword">set</span> { <span class="hljs-keyword">if</span>( index >= <span class="hljs-number">0</span> && index <= size<span class="hljs-number">-1</span> ) { namelist[index] = <span class="hljs-keyword">value</span>; } } } <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-keyword">this</span>[<span class="hljs-keyword">string</span> name] { <span class="hljs-keyword">get</span> { <span class="hljs-keyword">int</span> index = <span class="hljs-number">0</span>; <span class="hljs-keyword">while</span>(index < size) { <span class="hljs-keyword">if</span> (namelist[index] == name) { <span class="hljs-keyword">return</span> index; } index++; } <span class="hljs-keyword">return</span> index; } } <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"><span class="hljs-keyword">string</span>[] args</span>) </span>{ IndexedNames names = <span class="hljs-keyword">new</span> IndexedNames(); names[<span class="hljs-number">0</span>] = <span class="hljs-string">"Zara"</span>; names[<span class="hljs-number">1</span>] = <span class="hljs-string">"Riz"</span>; names[<span class="hljs-number">2</span>] = <span class="hljs-string">"Nuha"</span>; names[<span class="hljs-number">3</span>] = <span class="hljs-string">"Asif"</span>; names[<span class="hljs-number">4</span>] = <span class="hljs-string">"Davinder"</span>; names[<span class="hljs-number">5</span>] = <span class="hljs-string">"Sunil"</span>; names[<span class="hljs-number">6</span>] = <span class="hljs-string">"Rubic"</span>; <span class="hljs-comment">// 使用带有 int 参数的第一个索引器</span> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < IndexedNames.size; i++) { Console.WriteLine(names[i]); } <span class="hljs-comment">// 使用带有 string 参数的第二个索引器</span> Console.WriteLine(names[<span class="hljs-string">"Nuha"</span>]); Console.ReadKey(); } } } |
当上面的代码被编译和执行时,它会产生下列结果:
1 2 3 4 5 6 7 8 9 10 11 |
Zara Riz Nuha Asif Davinder Sunil Rubic N. A. N. A. N. A. <span class="hljs-number">2</span> |