Skip to content

Instantly share code, notes, and snippets.

@wullemsb
Last active November 9, 2025 12:48
Show Gist options
  • Select an option

  • Save wullemsb/67c7ba2ca4633478644d50ba1183b4f0 to your computer and use it in GitHub Desktop.

Select an option

Save wullemsb/67c7ba2ca4633478644d50ba1183b4f0 to your computer and use it in GitHub Desktop.
[CollectionBuilder(typeof(ImmutableStackBuilder), nameof(ImmutableStackBuilder.Create))]
public class ImmutableStack<T>
{
private readonly ImmutableList<T> _items;
internal ImmutableStack(){}
internal ImmutableStack(ImmutableList<T> items)
{
_items = items;
}
public T Peek() => _items[^1];
public ImmutableStack<T> Push(T item) => new(_items.Add(item));
public ImmutableStack<T> Pop() => new(_items.RemoveAt(_items.Count - 1));
public IEnumerator<T> GetEnumerator() => _items.GetEnumerator();
}
public static class ImmutableStackBuilder
{
public static ImmutableStack<T> Create<T>(ReadOnlySpan<T> items)
{
return new ImmutableStack<T>(ImmutableList.Create<T>(items));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment