My Own Stack c#

Key points

				
					using System;

namespace GenericMyOwnQueue
{
    class MyStack
    {
        private readonly int[] _items;
        private int _currentindex=-1;
        public MyStack() => _items = new int[10];
        public void Push(int item) => _items[++_currentindex] = item;
        public int Pop() => _items[_currentindex--];
        public int Count => _currentindex + 1;
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyStack stackObject = new MyStack();
            stackObject.Push(5);
            stackObject.Push(15);
            stackObject.Push(25);
            stackObject.Push(35);
            stackObject.Push(45);

            while (stackObject.Count > 0)
            {
                var item = stackObject.Pop();
                Console.WriteLine(item);
            }

            Console.ReadLine();
        }
    }
}

				
			

45

35

25

15

5

				
					 class MyStack
    {
        private readonly object[] _items;
        private int _currentindex=-1;
        public MyStack() => _items = new object[10];
        public void Push(object item) => _items[++_currentindex] = item;
        public object Pop() => _items[_currentindex--];
        public int Count => _currentindex + 1;
    }

    class MyStackString
    {
        private readonly string[] _items;
        private int _currentindex = -1;
        public MyStackString() => _items = new string[10];
        public void Push(string item) => _items[++_currentindex] = item;
        public object Pop() => _items[_currentindex--];
        public int Count => _currentindex + 1;
    }
				
			
				
					 class MyStack<T>
    {
        private readonly T[] _items;
        private int _currentindex=-1;
        public MyStack() => _items = new T[10];
        public void Push(T item) => _items[++_currentindex] = item;
        public T Pop() => _items[_currentindex--];
        public int Count => _currentindex + 1;
    }
				
			
				
					 class MyStack<T>
    {
        private readonly T[] _items;
        private int _currentindex=-1;
        public MyStack() => _items = new T[10];
        public void Push(T item) => _items[++_currentindex] = item;
        public T Pop() => _items[_currentindex--];
        public int Count => _currentindex + 1;
    }
				
			
				
					using System;

namespace GenericMyOwnQueue
{
    class MyStack<T>
    {
        private readonly T[] _items;
        private int _currentindex=-1;
        public MyStack() => _items = new T[10];
        public void Push(T item) => _items[++_currentindex] = item;
        public T Pop() => _items[_currentindex--];
        public int Count => _currentindex + 1;
    }

    class MyStackString
    {
        private readonly string[] _items;
        private int _currentindex = -1;
        public MyStackString() => _items = new string[10];
        public void Push(string item) => _items[++_currentindex] = item;
        public object Pop() => _items[_currentindex--];
        public int Count => _currentindex + 1;
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyStack<int> stackObject = new MyStack<int>();
            stackObject.Push(5);
            stackObject.Push(15);
            stackObject.Push(25);
            stackObject.Push(35);
            stackObject.Push(45);

            while (stackObject.Count > 0)
            {
                var item = stackObject.Pop();
                Console.WriteLine(item);
            }
            Console.WriteLine("--------------");
            MyStack<string> stackStrObject = new MyStack<string>();
            stackStrObject.Push("Hello");
            stackStrObject.Push("Hi");
            stackStrObject.Push("Anurag");
            stackStrObject.Push("how are");
            stackStrObject.Push("you ??");
            while (stackStrObject.Count > 0)
            {
                var item = stackStrObject.Pop();
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}

				
			

45
35
25
15
5
————–
you ??
how are
Anurag
Hi
Hello

Leave a Comment