public static void StackTest() { var stack = new ConcurrentStack <int>(); stack.Push(1); stack.Push(2); stack.Push(3); stack.Push(4); stack.Push(5); if (stack.TryPeek(out var result)) { Console.WriteLine($"{result} in on top"); } if (stack.TryPop(out var poped)) { Console.WriteLine($"{poped} pooped"); } var items = new int[5]; if (stack.TryPopRange(items, 0, 5) > 0) { var resulted = string.Join(", ", items.Select(i => i.ToString())); Console.WriteLine($"popped these items: {resulted}"); } }
static void Main() { var stack = new ConcurrentStack <int>(); stack.Push(1); stack.Push(2); stack.Push(3); stack.Push(4); int result; if (stack.TryPeek(out result)) { Console.WriteLine($"{result} is on top"); } if (stack.TryPop(out result)) { Console.WriteLine($"Popped {result}"); } var items = new int[5]; if (stack.TryPopRange(items, 0, 5) > 0) // actually pops only 3 items { var text = string.Join(", ", items.Select(i => i.ToString())); Console.WriteLine($"Popped these items: {text}"); } }
static void LIFO() { ConcurrentStack <int> stack = new ConcurrentStack <int>(); stack.Push(42); int result; if (stack.TryPop(out result)) { Console.WriteLine("Popped: { 0}", result); } stack.PushRange(new int[] { 1, 2, 3 }); int[] values = new int[2]; stack.TryPopRange(values); foreach (int i in values) { Console.WriteLine(i); } }