以前从来没有用过泛型,正在msdn上看泛型。看到上面的个例子。
public class GenericList<T> //: System.Collections.Generic.IEnumerable<T>  原来的例子上此处没屏蔽
    {
        // The nested class is also generic on T
        protected class Node
        {
            // T used in non-generic constructor
            public Node(T t)
            {
                next = null;
                data = t;
            }            private Node next;
            public Node Next
            {
                get { return next; }
                set { next = value; }
            }            // T as private member data type
            private T data;            // T as return type of property
            public T Data
            {
                get { return data; }
                set { data = value; }
            }
        }        protected Node head;        // constructor
        public GenericList()
        {
            head = null;
        }        // T as method parameter type:
        public void AddHead(T t)
        {
            Node n = new Node(t);
            n.Next = head;
            head = n;
        }
        
        //屏掉,迭代器foreach无效,并且必须得为公共的
        public IEnumerator<T> GetEnumerator()
        {
            Node current = head;            while (current != null)
            {
                yield return current.Data;
                current = current.Next;
            }
        }        //System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        //{
        //    return GetEnumerator();
        //}原来的例子上此处没屏蔽,为实现非泛型集合的迭代接口,个人觉得奇怪,此处定义的是泛型集口,可能出现非泛型的情况么? 请帮忙举个例子。
       
    }    public class SortedList<T> : GenericList<T> where T : System.IComparable<T>
    {
        // A simple, unoptimized sort algorithm that 
        // orders list elements from lowest to highest:        public void BubbleSort()
        {
            if (null == head || null == head.Next)
            {
                return;
            }
            bool swapped;            do
            {
                Node previous = null;
                Node current = head;
                swapped = false;                while (current.Next != null)
                {
                    //  Because we need to call this method, the SortedList
                    //  class is constrained on IEnumerable<T>
                    if (current.Data.CompareTo(current.Next.Data) > 0)
                    {
                        Node tmp = current.Next;
                        current.Next = current.Next.Next;
                        tmp.Next = current;                        if (previous == null)
                        {
                            head = tmp;
                        }
                        else
                        {
                            previous.Next = tmp;
                        }
                        previous = tmp;
                        swapped = true;
                    }
                    else
                    {
                        previous = current;
                        current = current.Next;
                    }
                }
            } while (swapped);
        }
    }    public class Person : System.IComparable<Person>
    {
        string name;
        int age;        public Person(string s, int i)
        {
            name = s;
            age = i;
        }        // This will cause list elements to be sorted on age values.
        public int CompareTo(Person p)
        {
            return age - p.age;
        }        public override string ToString()
        {
            return name + ":" + age;
        }        // Must implement Equals.
        public bool Equals(Person p)
        {
            return (this.age == p.age);
        }
    } static void Main(string[] args)
        {
            SortedList<Person> list = new SortedList<Person>();            //Create name and age values to initialize Person objects.
            string[] names = new string[] 
        { 
            "Franscoise", 
            "Bill", 
            "Li", 
            "Sandra", 
            "Gunnar", 
            "Alok", 
            "Hiroyuki", 
            "Maria", 
            "Alessandro", 
            "Raul" 
        };            int[] ages = new int[] { 45, 19, 28, 23, 18, 9, 108, 72, 30, 35 };            //Populate the list.
            for (int x = 0; x < 10; x++)
            {
                list.AddHead(new Person(names[x], ages[x]));
            }            //Print out unsorted list.
            foreach (Person p in list)
            {
                System.Console.WriteLine(p.ToString());
            }
            System.Console.WriteLine("Done with unsorted list");            //Sort the list.
            list.BubbleSort();            //Print out sorted list.
            foreach (Person p in list)
            {
                System.Console.WriteLine(p.ToString());
            }
            System.Console.WriteLine("Done with sorted list");            Console.ReadLine();        }

解决方案 »

  1.   

    不能继续接口就无法用foreach枚举
      

  2.   

    IEnumerable是为了实现IEnumerator GetEnumerator(),以便foreach当然,如果你不想foreach,那也可以不继续此接口
      

  3.   

    不知道你屏着试过没有。我把接口屏蔽掉完全可以使用foreach,只需要实现
     public IEnumerator<T> GetEnumerator()
            {
                Node current = head;            while (current != null)
                {
                    yield return current.Data;
                    current = current.Next;
                }
            }
    此方法就可以了。
      

  4.   

    也就是说在此实例中,
    System.Collections.Generic.IEnumerable<T> 这个接口完全没意义。
      

  5.   

    http://msdn.microsoft.com/zh-cn/library/0x6a29h6(v=VS.80).aspx
    这里就有个现成的例子,是没有继承System.Collections.Generic.IEnumerable<T>接口而可以使用迭代的。
    代码如下:
    // type parameter T in angle brackets
    public class GenericList<T> 
    {
        // The nested class is also generic on T
        private class Node
        {
            // T used in non-generic constructor
            public Node(T t)
            {
                next = null;
                data = t;
            }        private Node next;
            public Node Next
            {
                get { return next; }
                set { next = value; }
            }
            
            // T as private member data type
            private T data;        // T as return type of property
            public T Data  
            {
                get { return data; }
                set { data = value; }
            }
        }    private Node head;
        
        // constructor
        public GenericList() 
        {
            head = null;
        }    // T as method parameter type:
        public void AddHead(T t) 
        {
            Node n = new Node(t);
            n.Next = head;
            head = n;
        }    public IEnumerator<T> GetEnumerator()
        {
            Node current = head;        while (current != null)
            {
                yield return current.Data;
                current = current.Next;
            }
        }
    }
    class TestGenericList
    {
        static void Main()
        {
            // int is the type argument
            GenericList<int> list = new GenericList<int>();        for (int x = 0; x < 10; x++)
            {
                list.AddHead(x);
            }        foreach (int i in list)
            {
                System.Console.Write(i + " ");
            }
            System.Console.WriteLine("\nDone");
        }
    }
      

  6.   

    http://www.cnblogs.com/jeffreyzhao/archive/2009/05/29/generic-performance-test.html参考这个
      

  7.   

    GenericList<T> 是不是已经隐式的继续了IEnumerable<T> 我是这么理解的
      

  8.   

    我也有这种感觉,要不看着也太糊涂了。。这还是msdn上的,但是我看了下泛型的介绍,也没说它隐式继承了哪些接口。
      

  9.   

    public IEnumerator<T> GetEnumerator()这个方法很诡异,改什么东西都会出错。
      

  10.   

    public IEnumerator<T> GetEnumerator()这个是用来枚举的
      

  11.   

     //System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            //{
            //    return GetEnumerator();
            //}这个是为了能foreach
      

  12.   

    有可能啊,你参数可以不用T,用object,泛型有规定不可以用object吗
      

  13.   

    重载一个   
         public void AddHead(object o)
            {
                Node n = new Node(o);
                n.Next = head;
                head = n;
            }
      

  14.   

    你说的是这啊,那你用object,不是违返了使用泛型的本意了么,再说了,t可以满足任何类型啊。使用object实在是多此一举。还有,你再看看后面说的问题,后面有个例子是不继承IEnumerable<T>此接口,依然是可以使用foreach迭代的。
      

  15.   


    如果对集合进行了更改(如添加、修改或删除元素),则枚举数将失效且不可恢复,并且下一次对 MoveNext 或 Reset 的调用将引发 InvalidOperationException。
      

  16.   

    楼主,你试试你写的这个类的实例能不能转化成IEnumerable<T>,如果能应该就是默认实现了,不能的话就得问微软了
      

  17.   

    我使用强制转换试了下,不能转化成IEnumerable<T>的实似
      

  18.   

    泛型本来就是继承了IEnumerable的
      

  19.   

    因为 IEnumerable<T> 继承于 IEnumerable,所以你必须实现 IEnumerable 的成员:IEnumerator GetEnumerator(),就这么简单。