using System.Collections.Generic;
// 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;
            
            System.Console.Write("\n"+current.Data.ToString()+"\n");
        }
    }
}class TestGenericList
{
    static void Main()
    {
        // int is the type argument
        GenericList<int> l = new GenericList<int>();        for (int x = 0; x < 10; x++)
        {
            l.AddHead(x);
        }        foreach (int i in l)
        {
            System.Console.Write(i + " ");
        }
        System.Console.WriteLine("\nDone");
    }
}=============================================================================
执行结果:
    9 8 7 6 5 4 3 2 1 0
   Done
谁可以为我讲解一下??
尤其关于GetEnumerator()这个方法,在这段代码中是如何调用得?

解决方案 »

  1.   

    public IEnumerator<T> GetEnumerator()
        {
            Node current = head;        while (current != null)
            {
                yield return current.Data;
                current = current.Next;
                
                //System.Console.Write("\n"+current.Data.ToString()+"\n");
            }
        }
    }=============================================================
    改一下:
    System.Console.Write("\n"+current.Data.ToString()+"\n");
    这句注释掉,这句是我自己加得 - -   
      

  2.   

    GetEnumerator()   迭代
    循环读取相应集合元素值
    public IEnumerator<T> GetEnumerator()
        {
            Node current = head;        while (current != null)
            {
                yield return current.Data;
                current = current.Next;///下移
                
                System.Console.Write("\n"+current.Data.ToString()+"\n");
            }
        }
    }
    foreach方法就是继承这个接口的.
      

  3.   

    using System.Collections.Generic;
    // 此处应该可以认为是一个模板类、通用类,此类可以接收任意的类型,所以用T来代替,你可以写成别的什么字母
    public class GenericList<T> 
    {
        // The nested class is also generic on T
        private class Node
        {
            // 构造函数
            public Node(T t)
            {
                next = null;
                data = t;
            }
            // 是Node类的一个属性。下面是属性的标准定义方法
            private Node next;
            public Node Next
            {
                get { return next; }
                set { next = value; }
            }
            
            // 同上面的Next相同,都是属性
            private T data;        // T as return type of property
            public T Data  
            {
                get { return data; }
                set { data = value; }
            }
        }
        // 相当于都指针的概念,
        private Node head;
        
        // 
        public GenericList() 
        {
            head = null;
        }    // 对节点进行添加
        public void AddHead(T t) 
        {
            Node n = new Node(t);
            n.Next = head;
            head = n;
        }
        //返回节点集合用到了IEnumerator接口
        public IEnumerator<T> GetEnumerator()
        {
            Node current = head;        while (current != null)
            {
                yield return current.Data;
                current = current.Next;
                
                System.Console.Write("\n"+current.Data.ToString()+"\n");
            }
        }
    }class TestGenericList
    {
        static void Main()
        {
            // 用int类型来创建一个链表,此处的<int>就是在用来替换上面的<T>的,你也可以用<float>、<double>、<unit>、<自己定义的类>等等等等
            GenericList<int> l = new GenericList<int>();        for (int x = 0; x < 10; x++)
            {
                l.AddHead(x);
            }        foreach (int i in l)
            {
                System.Console.Write(i + " ");
            }
            System.Console.WriteLine("\nDone");
        }
    }
    此实例的作用就是创建一个链表,只是将创建链表的方法做成了模板,做成了通用类,以后无论是什么类型的链表都可以用这个GenericList类,
    GetEnumerator方法其实是返回一个迭代器,用于迭代链表中的所有元素。
      

  4.   

    首先得弄清楚什么是泛型这个例子是生成一个单向链表并实现了IEnumerator接口用于枚举内部数据
            Node current = head;        while (current != null)
            {
                yield return current.Data;
                current = current.Next;
                
                //System.Console.Write("\n"+current.Data.ToString()+"\n");
            }从链表头开始,每获取完一个元素后,将该元素指向下一个元素,直到达尾
      

  5.   


      明白了~
      该实例使用了 foreach 访问集合类得不兼容类型
       已经找到答案
       谢谢~