Created
February 9, 2021 20:29
-
-
Save mttchpmn/706d1edb49f798db9212230209c8704a to your computer and use it in GitHub Desktop.
C# Stack Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace Sandbox | |
{ | |
public class Stack<T> | |
{ | |
private readonly List<T> _list = new List<T>(); | |
public void Push(T obj) | |
{ | |
_list.Add(obj); | |
} | |
public T Pop() | |
{ | |
if (_list.Count < 1) throw new InvalidOperationException("No items in stack."); | |
var lastItem = _list[^1]; | |
_list.RemoveAt(_list.Count - 1); | |
return lastItem; | |
} | |
public void Clear() | |
{ | |
_list.Clear(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment