RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
一篇文章带你了解C#索引器

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

在湖口等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站建设、成都网站建设 网站设计制作按需搭建网站,公司网站建设,企业网站建设,品牌网站制作,成都营销网站建设,外贸网站建设,湖口网站建设费用合理。

概述

索引器(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),您可使用 get 和 set 访问器来定义索引器。但是,属性返回或设置一个特定的数据成员,而索引器返回或设置对象实例的一个特定值。换句话说,它把实例数据分为更小的部分,并索引每个部分,获取或设置每个部分。

定义一个属性(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. } 

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

 
 
 
  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.  
  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. } 

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

 
 
 
  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 

标题名称:一篇文章带你了解C#索引器
文章地址:http://aqpgk.com/article/cdcgcog.html