首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

哪位高手来帮小弟我看一下《C#2.0程序设计教程》中一个例题的异常呀

2012-01-24 
谁来帮我看一下《C#2.0程序设计教程》中一个例题的错误呀//程序清单P14_1.cs:usingSystemusingSystem.Colle

谁来帮我看一下《C#2.0程序设计教程》中一个例题的错误呀
//程序清单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.cs2918ConsoleApplication2

我是个C#半新手,今天我照着书上的这个程序编写了一段代码,也是这个错误,开始我以为是自己哪个程序中其它地方写错了,结果把书上的这段代码原封不动的运行也出现上面一样的错误,谁帮我看看是什么地方不对呀???????谢谢了

[解决办法]

IEnumerator <T> IEnumerable <T> .GetEnumerator()
{
return GetEnumerator();
}
中的..return GetEnumerator();改成
return m_list.GetEnumerator(); 看看..不知道行不行
[解决办法]
using System.Collections;

热点排行