代码如下。问题也如下:
1. 如何遍历对象p 输出 p的每个值。(foreach (object o in d.ar)可以实现,但是就想遍历对象p)
2. public Object this[int index]{} 这个函数的写法,请详细讲讲。(功能我知道)
class Program 
    {
        protected ArrayList ar = new ArrayList();
        public Object this[int index]
        {
            get
            {
                if (index >= 0 && index < ar.Count)
                {
                    return ar[index];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                ar.Add(value);
            }
        }        static void Main(string[] args)
        {
            Program d = new Program();
            d[0] = "111";
            d[3] = 222;
            foreach (object o in d) //有问题,出错。
            {
                Console.WriteLine(o.ToString());            }
            
            Console.ReadKey();
        }    }

解决方案 »

  1.   

    Program d = new Program();
                d[0] = "111";
                d[3] = 222;
    ArrayList()貌似不支持空元素不大明白你第一个问题什么意思
    public Object this[int index]{} 这个是给结合添加索引而不是方法
      

  2.   

    索引器允许类或结构的实例按照与数组相同的方式进行索引。索引器类似于属性,不同之处在于它们的访问器采用参数。在下面的示例中,定义了一个泛型类,并为其提供了简单的 get 和 set 访问器方法(作为分配和检索值的方法)。Program 类为存储字符串创建了此类的一个实例。C#  复制代码 
    class SampleCollection<T>
    {
        private T[] arr = new T[100];
        public T this[int i]
        {
            get
            {
                return arr[i];
            }
            set
            {
                arr[i] = value;
            }
        }
    }// This class shows how client code uses the indexer
    class Program
    {
        static void Main(string[] args)
        {
            SampleCollection<string> stringCollection = new SampleCollection<string>();
            stringCollection[0] = "Hello, World";
            System.Console.WriteLine(stringCollection[0]);
        }
    } 索引器概述
    索引器使得对象可按照与数组相似的方法进行索引。get 访问器返回值。set 访问器分配值。this 关键字用于定义索引器。value 关键字用于定义由 set 索引器分配的值。索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。索引器可被重载。索引器可以有多个形参,例如当访问二维数组时。如果你要用foreach的话,你的类要继承IEnumerable
      

  3.   


    谢谢!大概知道我的代码是什么了,但是我想遍历输出p的话,改怎么遍历?
    比如您说“如果你要用foreach的话,你的类要继承IEnumerable",那具体代码该怎么写?
      

  4.   


    class Program :IEnumerable
    {
    .....
    public IEnumerator GetEnumerator ()
    {
    for(int i=0;i<ar.size;i++)
    {
    yield return ar[i];
    }
    }
    }基本上是这个样子吧,没有编译
      

  5.   

    想遍历的话要实现IEnumerable接口 并实现自己的IEnumerator
    按你的样子写了一段代码 测试可以过 public class A : IEnumerable
            {
                public ArrayList ar = new ArrayList();            public object this[int index]
                {
                    get
                    {
                        if (index >= 0 && index < ar.Count)
                        {
                            return ar[index];
                        }
                        else
                        {
                            return null;
                        }                }
                    set
                    {
                        ar.Add(value);
                    }
                }            //   IEnumerable   Interface   Implementation:   
                //       Declaration   of   the   GetEnumerator()   method     
                //       required   by   IEnumerable   
                public IEnumerator GetEnumerator()
                {
                    return new TokenEnumerator(this);
                }
                private class TokenEnumerator : IEnumerator
                {
                    private int position = -1;
                    private A t;                public TokenEnumerator(A t)
                    {
                        this.t = t;
                    }                //   Declare   the   MoveNext   method   required   by   IEnumerator:   
                    public bool MoveNext()
                    {
                        if (position < t.ar.Count - 1)
                        {
                            position++;
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }                //   Declare   the   Reset   method   required   by   IEnumerator:   
                    public void Reset()
                    {
                        position = -1;
                    }                //   Declare   the   Current   property   required   by   IEnumerator:   
                    public object Current
                    {
                        get
                        {
                            return t.ar[position];
                        }
                    }
                }        } A atest = new A();
                atest[0] = "1";
                atest[1] = "1";            foreach (object o in atest)
                {
                    string stest = o.ToString();
                }
      

  6.   

    用foreach遍历自然是最方便的
    foreach(Object ob in d)
    {
       ob.属性
    }
    更多功能:
    如果需要查找某值或排序的话用linq的查询语句是非常方便的
    var result=
        from ob in d
        where ob.属性=属性值
        select ob
      

  7.   

    才注意到你的class Program 没继承IEnumerable接口
    汗~~~
      

  8.   


    您这个不对吧,i<ar.size ,ar是arraylist,foreach循环直接就可以用,我想遍历 对象p
      

  9.   

    如6楼实现IEnumerable接口就行了
      

  10.   


    谢谢!您写的代码我看懂了,继承IEnumerable接口,就要把它的4个方法都实现了是把?不实现的话不能直接用? 那这也太麻烦了有没有简单点的代码直接就可以遍历对象p?