一样的,也称索引器,这个主要是用于类中包含集合,用下标方式访问比较方便,就在类中定义索引器,和数组有很明显的区别,不是类的数组(包含多个类元素),而是一个类的多个元素,象.net framework中的集合类,本身只是一个类。

解决方案 »

  1.   

    1 cusing System;
    2 public class ixtest
    3 {
    4 private int[] Array=new int[10];
    5 public int this [int index]
    6 {
    7 get
    8 {
    9 if(index>=0 || index<10)
    10    return Array[index];
    11 else
    12 return 0;
    13 }
    14 set
    15 {
    16 if(index>=0||index<10)
    17 Array[index]=value;
    18 }
    19 }
    20 }
    21 public class test
    22 {
    23 public static void Main()
    24 {
    25 ixtest t=new ixtest();
    26 t[3]=256;
    27 t[9]=1024;
    28 for(int i=0;i<10;i++)
    29 {
    30 Console.Write("{0,5:d}",t[i]);
    31 }
    32
    33 }
    34 }索引的声明形式:访问修饰符 类型 this [索引值] { 代码}1、在上面的例子中,26行中:表明在test类 创建ixtest的对象t,26,27行以t[]这方式来操作其内的数组,如果在第4行的地方声明了多个数组,那t[]调用到的是哪一个数组?2、若在上例ixtest中(如第四行)声明了多个数组,为每一个数组声明索引时,还是象第5行一样只能用this 这个关键字吗?(如上面的索引的声明形式)