泛型类
View Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace Ienumerable { class Program { static void Main(string[] args) { MyList<int> mylist = new MyList<int>(); for (int x = 0; x < 5; x++) { mylist.Add(x); } foreach (int i in mylist) { Console.WriteLine(i); } Console.ReadKey(); } } class MyList<T>:IEnumerable { #region MyItem private class MyItem { private MyItem next; private T data; public MyItem(T t) { next = null; data = t; } public MyItem Next { get { return next; } set { next = value; } } public T Data { get { return data; } set { data = value; } } } #endregion private MyItem myItem; public MyList() { myItem = null; } public void Add(T t) { MyItem n = new MyItem(t); n.Next = myItem; myItem = n; } #region IEnumerable 成员 public IEnumerator<T> GetEnumerator() { MyItem currentItem = myItem; while (currentItem != null) { yield return currentItem.Data; currentItem = currentItem.Next; } } #endregion } }