public class ClassItem
{
        private string _name = string.Empty;
        public string name
        {
            get
            {
                return _name;
            }
            set 
            {
                _name = value;
            }
        }
}
    public class ClassCollections : Collection<ClassItem>
    {
        public ClassItem this[int index]
        {
            get
            {
                return this[index];
            }
        }    }我建了两个类,如上代码,下面是入口ClassItem item1 = new ClassItem();
                ClassItem item2 = new ClassItem();
                ClassItem item3 = new ClassItem();
                ClassCollections collection = new ClassCollections();
                item1.name = "item1";
                item2.name = "item2";
                item3.name = "item3";
                collection.Add(item1);
                collection.Add(item2);
                collection.Add(item3);
                Console.WriteLine("item1:{0}", collection[0].name);
                Console.WriteLine("item2:{0}", collection[1].name);
                Console.WriteLine("item3:{0}", collection[2].name);运行到
                Console.WriteLine("item1:{0}", collection[0].name);
这里的时候,我调试进去看,为什么会一直循环执行
public ClassItem this[int index]
        {
            get
            {
                return this[index];
            }
        }
这一段代码,而且index一直是0呢?

解决方案 »

  1.   


    public class ClassCollections : Collection<ClassItem>
    {
        public new ClassItem this[int index]
        {
            get
            {
                return base[index];//调用父类的索引器,否则就递归调用自己了。
            }
        }
    }
      

  2.   

    public ClassItem this[int index]定义了一个索引运算符。当用this[index]调用时,造成递归。
      

  3.   

    我是看了一下别人写的一段代码,然后自己模仿那种写法过来调试,但是想不明白,还是不太理解这样写是做什么用的,MSDN上的例子是class IndexerClass
    {
        private int[] arr = new int[100];
        public int this[int index]   // Indexer declaration
        {
            get
            {
                // Check the index limits.
                if (index < 0 || index >= 100)
                {
                    return 0;
                }
                else
                {
                    return arr[index];
                }
            }
            set
            {
                if (!(index < 0 || index >= 100))
                {
                    arr[index] = value;
                }
            }
        }
    }class MainClass
    {
        static void Main()
        {
            IndexerClass test = new IndexerClass();
            test[3] = 256;
            test[5] = 1024;
            for (int i = 0; i <= 10; i++)
            {
                System.Console.WriteLine("Element #{0} = {1}", i, test[i]);
            }
        }
    }它这个我看得懂,主要是test类里面还有一个私有的int数组,this[index]就是直接用的那个int数组,但是我在网上看别人的一个代码,他里面都没有这个,那他的this[index]是取的哪里的值就搞不懂了
    如下是我理解不了的代码    public class ToolBoxItem
        {
            private String _name = "";
            private Int32 _imageIndex = -1;
            private ToolBoxItem _parent = null;        public String Name
            {
                get
                {
                    return _name;
                }
                set
                {
                    _name = value;
                }
            }        public Int32 ImageIndex
            {
                get
                {
                    return _imageIndex;
                }
                set
                {
                    _imageIndex = value;
                }
            }        [Browsable(false)]
            public ToolBoxItem Parent
            {
                get
                {
                    return _parent;
                }
                set
                {
                    _parent = value;
                }
            }
        }public class ToolBoxItemCollection : Collection<ToolBoxItem>
        {
            public event CollectionChangeEventHandler ItemChanged;        public ToolBoxItemCollection()
            {
                ItemChanged += new CollectionChangeEventHandler(OnToolBoxItemCollectionChanged);
            }        public ToolBoxItem this[String name]
            {
                get
                {
                    Int32 index = -1;
                    for (Int32 i = 0; i < this.Count; i++)
                    {
                        if (this[i].Name == name)
                        {
                            index = i;
                            break;
                        }
                    }                if (index >= 0 && index < this.Count)
                    {
                        return this[index];
                    }                return null;
                }
            }    }他这代码应该怎么去理解他的思路,我想了好久还是没想明白
      

  4.   

    抽开这些,单独给个例子不知道你是否能容易理解一些。
    public class IndexTest
    {
        public string this[int index]
        {
            get
            {
                return "yes , you get a item by this["+index.ToString()+"]";
            }
            set
            {
                Console.WriteLine("You set " + value + " for item["+index.ToString()+"]");
            }
        }
    }public void Test()
    {
        IndexTest obj = new IndexTest();
        obj[0] = "first";
        obj[1] = "second";
        Console.WriteLine(obj[100]);
    }这是纯粹演示索引器了。没有牵扯集合,继承,等问题。比较简单。
      

  5.   

    to 逍遥兄在我说的那个例子那里,他在ToolBoxItemCollection类里有这样一个方法
    protected override void InsertItem(int index, ToolBoxItem item)
            {
                base.InsertItem(index, item);
                ItemChanged(this, new CollectionChangeEventArgs(CollectionChangeAction.Add, item));
            }
    是不是这个方法是往ToolBoxItemCollection类里添加ToolBoxItem 类型的对象进去,然后通过索引器把添加进去的对象取出来?