索引器的行为更像一个支持IEnumerator接口和IEnumerable接口的自定义集合。唯一的主要区别是:和使用接口类型访问内容相比,我们可以向操作一个标准数组一样操作索引器。

    索引器的创建方式:
   

{
   索引器的构建
   //使用ArrayList作为Car类型的容器
   private ArrayList carArray = new ArrayList();
   
//索引器基于数字序号返回一个Car
   public Car this[int pos]
   {
        
//注意ArrayList也有一个索引器
         get { return (Car)carArray[pos]; }
        
set { carArray[pos] = value; }
   }
}

相关文章:

  • 2021-10-09
  • 2021-05-03
  • 2021-05-23
  • 2021-06-11
  • 2021-06-17
  • 2021-07-10
  • 2022-12-23
猜你喜欢
  • 2021-10-30
  • 2022-12-23
  • 2021-07-17
  • 2021-09-11
  • 2021-12-20
  • 2021-09-15
  • 2022-12-23
相关资源
相似解决方案