Disposable Enumerator



#region Using directives


using System;


using System.Collections.Generic;


using System.Text;


#endregion


namespace ConsoleApplication1


{


class Program


{


static void Main(string[] args)


{


MyList<string> l = new MyList<string>();


l.Add (“hello”);


l.Add (“wild”);


l.Add (“world”);


l.Add (“!”);


foreach (string s in l) {


Console.WriteLine(s);


}


Console.ReadLine();


}


}


public class MyList<T> : IList<T>


{


List<T> list;


public MyList()


{


list = new List<T>();


}


public void Clear()


{


list.Clear();


}


public bool Contains(T item)


{


return list.Contains(item);


}


public void CopyTo(T[] array, int arrayIndex)


{


list.CopyTo(array, arrayIndex);


}


public bool Remove(T item)


{


return list.Remove(item);


}


public int Count


{


get


{


return list.Count;


}


}


public bool IsReadOnly


{


get


{


return false;


}


}


public T this[int index]


{


get


{


return list[index];


}


set


{


list[index] = value;


}


}


public int IndexOf (T item) {


return list.IndexOf(item);


}


public void Insert (int index, T item) {


list.Insert(index, item);


}


public void Add (T item) {


list.Add(item);


}


public void RemoveAt (int index) {


list.RemoveAt(index);


}


public IEnumerator<T> GetEnumerator () {


return new DisposableEnumerator(list.GetEnumerator());


}


public void Dispose () {


Console.WriteLine(“In Dispose”);


}


internal class DisposableEnumerator: IEnumerator<T>, IDisposable {


IEnumerator<T> enumerator;


public DisposableEnumerator(IEnumerator<T> l)


{


enumerator = l;


}


public bool MoveNext () {


return enumerator.MoveNext();


}


public T Current


{


get


{


return enumerator.Current;


}


}


public void Dispose()


{


Console.WriteLine(“In Dispose”);


}


 


}


 


}


}

Comments (1)

Skip to main content