返回一个数组的方法是用接口索引
在类array中加this索引代码
     public double this[int i]
            {
                get
                {
                    return array[i];
                }
            }
创建类实例后,直接引用 array[i]
现在我array类中有多个数组需要返回,有什么好的方法呢?
   

解决方案 »

  1.   

    public double this[int i]
                {
                    get
                    {
                        return array[i];
                    }
                }
    你这个时返回数组么-_-#多个数组
         public double[] this[int[] i]
                {
                    get
                    {
                        //处理
                    }
                }
      

  2.   

    那就不是写索引器了
    应该用一个属性或者方法来实现
    返回一个 collection 对象
      

  3.   

    觉得返回一个数组和用索引不是一回事.如果你只是想返回一个数组那么可以直接定义数组类型的属性返回就行了:public int[] getIntArray
    {
    get
    {
    return this.m_array;
    }
    }
    //或
    public int[] getIntArray
    {
    get
    {
    int[] _array = new int[m_array.Length];
    m_array.CopyTo(_array, 0);
    return this._array;
    }
    }
      

  4.   

    比如我的array类中经过计算后已经得到 3个数组array1[],array2[],array3[],三个没什么相关性(既无法根据条件判断返回哪个,因为3个都要返回),我在类外部要全用到这三个数组,用索引的话可以很方便的return其中一个以类为名称的数组,不是我要的目的。
      

  5.   

    那你可以在索引上再加一个参数来表示要取哪一个数组的值,如:
    public double this[int index, int option]
    {
    get
    {
    switch (option)

    case 1:
    return array1[index];
    case 2:
    return array2[index];
    default:
    return array3[index];
    }
    }
    }
      

  6.   

    hbxtlhx的可以,不好意思我刚开始用,数组的这个用法都没查到,谢谢
    怎么给分?