//程序清单P14_1.cs:
using System;
using System.Collections.Generic;namespace P14_1
{
    public class AssembleIteratorSample
    {
        static void Main()
        {
            Assemble<int> iAsm = new Assemble<int>();
            iAsm.Add(4);
            iAsm.Add(12);
            iAsm.Add(9);
            iAsm.Add(1);
            iAsm.Add(8);
            Console.WriteLine("排序前:");
            foreach (int i in iAsm)
                Console.WriteLine(i);
            Console.WriteLine("排序后:");
            foreach (int i in iAsm.SortedEnumerator())
                Console.WriteLine(i);
        }
    }    /// <summary>
    /// 泛型类: 集合Assemble
    /// </summary>
    public class Assemble<T> : IEnumerable<T>
        where T : IComparable<T>
    {
        //字段
        protected T[] m_list;
        protected int m_count = 0;
        protected int m_capacity = 100;        //属性
        public int Length
        {
            get
            {
                return m_count;
            }
        }        public int Capacity
        {
            get
            {
                return m_list.Length;
            }
        }        //索引函数
        public T this[int index]
        {
            get
            {
                return m_list[index];
            }
            set
            {
                m_list[index] = value;
            }
        }        //构造函数
        public Assemble()
        {
            m_list = new T[m_capacity];
        }        public Assemble(int iCapacity)
        {
            m_capacity = iCapacity;
            m_list = new T[m_capacity];
        }        //方法
        public void Add(T tp)
        {
            if (m_count < m_list.Length)
                m_list[m_count++] = tp;
        }        public void Remove(T tp)
        {
            int index = this.Find(tp);
            if (index == -1)
                return;
            this.RemoveAt(index);
        }        public void RemoveAt(int index)
        {
            for (int i = index; i < m_count - 1; i++)
            {
                m_list[i] = m_list[i + 1];
            }
            m_list[--m_count] = default(T);
        }        public int Find(T tp)
        {
            for (int i = 0; i < m_count; i++)
                if (m_list[i].Equals(tp))
                    return i;
            return -1;
        }        public void Sort()
        {
            T tmp;
            for (int i = m_count - 1; i > 0; i--)
            {
                for (int j = 0; j < i; j++)
                {
                    if (this[j].CompareTo(this[j + 1]) > 0)
                    {
                        tmp = this[j + 1];
                        this[j + 1] = this[j];
                        this[j] = tmp;
                    }
                }
            }
        }        public IEnumerator<T> GetEnumerator()
        {
            for (int i = 0; i < m_count; i++)
                yield return m_list[i];
        }        public IEnumerable<T> SortedEnumerator()
        {
            int[] index = new int[m_count];
            for (int i = 0; i < m_count; i++)
                index[i] = i;
            int iTmp;
            for (int i = m_count - 1; i > 0; i--)
            {
                for (int j = 0; j < i; j++)
                {
                    if (m_list[index[j]].CompareTo(m_list[index[j + 1]]) > 0)
                    {
                        iTmp = index[j + 1];
                        index[j + 1] = index[j];
                        index[j] = iTmp;
                    }
                }
            }
            for (int i = 0; i < m_count; i++)
                yield return m_list[index[i]];
        }
    }
}错误 1 “P14_1.Assemble<T>”不会实现接口成员“System.Collections.IEnumerable.GetEnumerator()”。“P14_1.Assemble<T>.GetEnumerator()”或者是静态、非公共的,或者有错误的返回类型。 D:\My Documents\Visual Studio 2005\Projects\ConsoleApplication2\Program.cs 29 18 ConsoleApplication2我是个C#半新手,今天我照着书上的这个程序编写了一段代码,也是这个错误,开始我以为是自己哪个程序中其它地方写错了,结果把书上的这段代码原封不动的运行也出现上面一样的错误,谁帮我看看是什么地方不对呀???????谢谢了

解决方案 »

  1.   

    在Assemblt<T>类的结尾加上:
        IEnumerator IEnumerable.GetEnumerator()
        {
          return GetEnumerator();
        }
    是写书的人想当然犯的错误。
      

  2.   

    顶一下, 上次在Microsoft Press的书上也看到了错误, 不过是印刷错误.
      

  3.   

    to:Ivony(授人以鱼不如授人以渔,上海谋生) :
    谢谢解答,我在后面添加了你说的那些代码,可是还是不行呀
    我添加如下代码的话问题依旧噢~~IEnumerator<T> IEnumerable<T>.GetEnumerator()
            {
                return GetEnumerator();
            }麻烦你再帮我看一下好吗???
      

  4.   

    把 
    IEnumerator<T> IEnumerable<T>.GetEnumerator()
            {
                return GetEnumerator();
            }
    中的..return GetEnumerator();改成
    return m_list.GetEnumerator(); 看看..不知道行不行
      

  5.   

    直接把我那段代码Copy进去就行了,IEnumerable<T>继承于IEnumerable?为什么不自己先试一下?还是看花了眼?
      

  6.   

    直接拷那段代码不行的呀
    他提示
    错误 1 使用泛型 类型“System.Collections.Generic.IEnumerator<T>”需要“1”个类型参数 D:\My Documents\Visual Studio 2005\Projects\ConsoleApplication2\Program.cs 156 9 ConsoleApplication2错误地方就是IEnumerator IEnumerable.GetEnumerator()中的IEnumerator那里噢
    我是在VS2005里面调试的
      

  7.   

    using System.Collection
    VS2005那里直接有个提示没看见……