IENUMERABLE接口实现:

IENUMERABLE接口实现:

class Program
    {
        static void Main(string[] args)
        {          
            WareHouse house = new WareHouse();
            house.Add("1");
            house.Add("2");
            house.Add("3");
            foreach (string s in house)
            {
                Console.Write(string.Format("{0}\n", s));
            }
            Console.ReadLine();
        }
    }   

    public class WareHouse : IEnumerable
    {
        private List t_List = new List();
      

        public void Add(T obj)
        {
            t_List.Add(obj);
        }

        public void DeleteAt(int i)
        {
            t_List.RemoveAt(i);
        }       

        private class GarageEnumerator : IEnumerator
        {
            private int m_CurrentIndex = -1;
            private WareHouse m_house = null;

            public GarageEnumerator(WareHouse house)
            {
                m_house = house;
            }

            object System.Collections.IEnumerator.Current
            {
                get { return m_house.t_List[m_CurrentIndex]; }
            }        

            #region IEnumerator 成员

            T System.Collections.Generic.IEnumerator.Current
            {
                get { return m_house.t_List[m_CurrentIndex]; }
            }

            #endregion

            public bool MoveNext()
            {
                m_CurrentIndex++;
                if (m_CurrentIndex >= m_house.t_List.Count)
                {
                    m_CurrentIndex--;
                    return false;
                }
                return true;
            }

            public void Reset()
            {
                m_CurrentIndex = 0;
            }

            public void Dispose()
            {
            }           
        }
        public IEnumerator GetEnumerator()
        {
            return new GarageEnumerator(this);
        }
    }

你可能感兴趣的:(C#)