Skip to content

Instantly share code, notes, and snippets.

@mttchpmn
Created February 9, 2021 20:29
Show Gist options
  • Save mttchpmn/706d1edb49f798db9212230209c8704a to your computer and use it in GitHub Desktop.
Save mttchpmn/706d1edb49f798db9212230209c8704a to your computer and use it in GitHub Desktop.
C# Stack Class
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