自定义集合类推荐你从 ICollection<T> 继承来做

解决方案 »

  1.   

    public int Current 
          {
             get 
             {
                return(collection.items[nIndex]);
             }
          }
    属性返回都是int,怎么会返回object?
      

  2.   

    public int Current
    =================
    你返回的是整型啊
      

  3.   

    public int Current 
          {
             get 
             {
                return(collection.items[nIndex]);
             }
          }
    返回INT
      

  4.   

    汗,你定义的属性都是int类型,返回的值当然也是int类型,怎么会是object类型呢?
      

  5.   

    MyCollection col = new MyCollection();
         foreach (int i in col) 
          {
             Console.WriteLine(i);
          }
    col是一个MyCollection 类型的对象,foreach (int i in col)中你都定义了int i了,返回的当然是i
      

  6.   

    是的,谢谢楼上的回复。我的问题其实就是 MyEnumerator: IEnumerator  既然继承了接口IEnumerator  ,那为什么FOREACH不使用接口属性
    object IEnumerator.Current 
          {
             get 
             {
                return(Current);
             }
          }
    而要使用自己定义的属性。
    public int Current 
          {
             get 
             {
                return(collection.items[nIndex]);
             }
          }那继承接口还有什么意义。另外  PCI_E(天上星) ( ) 信誉:100    Blog   加为好友  2007-6-29 10:37:36  得分: 0  
    MyCollection col = new MyCollection();
         foreach (int i in col) 
          {
             Console.WriteLine(i);
          }
    col是一个MyCollection 类型的对象,foreach (int i in col)中你都定义了int i了,返回的当然是i这里和FOREACH里面定义应该int没关系,不是定义了int 就返回int,你定义成CHAR,或者string 编译会提示无法将类型 int 转换成 string  
     
      

  7.   

    刚才有自己调试了一下,发现。当没有自定义的属性
    public int Current 
          {
             get 
             {
                return(collection.items[nIndex]);
             }
          }
    在使用FOREACH的时候,调用的是显示接口声明的属性
    object IEnumerator.Current 
          {
             get 
             {
                return(Current);
             }
          }要是同时存在自己定义的属性,和显示接口实现的属性时候。FOREACH使用的是自己定义的属性。