using System.Collections;
using System.Collections.Generic;
public class MyEnumerator<T> : IEnumerator<T>
private MyStack<T> stack;
public MyEnumerator(MyStack<T> stack)
public T Current => stack[position];
object System.Collections.IEnumerator.Current => stack[position];
public class MyStack<T> : IEnumerable<T>
private const int DEFAULT_SIZE = 10;
array = new T[DEFAULT_SIZE];
public MyStack<T> Push(T value)
T[] newArray = new T[array.Length * 2];
Array.Copy(array, newArray, array.Length);
throw new InvalidOperationException("Stack is empty");
throw new InvalidOperationException("Stack is empty");
public IEnumerator<T> GetEnumerator()
return new MyEnumerator<T>(this);
IEnumerator IEnumerable.GetEnumerator()
return new MyEnumerator<T>(this);
if(i < 0 || i > this.Count)
throw new IndexOutOfRangeException("Trying to reach non-existing element of Stack");
public static void Main()
Console.WriteLine("---My Stack---");
var t = new MyStack<int>();
t.Push(1).Push(1).Push(1).Push(2).Push(3).Push(4).Push(5).Push(6).Push(7).Push(8).Push(9).Push(10).Push(10).Push(10);
Console.WriteLine($"\n{t.Pop()}\n{t.Pop()}\n{t.Peek()}\n");
t.Push(9).Push(10).Push(11);
Console.WriteLine("\n\n");
Console.WriteLine("---Out of the box Stack---");
var p = new Stack<int>();
for(int i = 1; i <= 10; i++)
Console.WriteLine($"\n{p.Pop()}\n{p.Pop()}\n{p.Peek()}\n");