using System;
using System.Collections.Concurrent;
class Program
{
static void Main()
{
// Create ConcurrentStack from array of elements.
int[] elements = { 50, 10, 0 };
ConcurrentStack<int> stack = new ConcurrentStack<int>(elements);
Console.WriteLine(string.Join(
",", stack.ToArray()));
// Push a new value to the stack.
stack.Push(2000);
Console.WriteLine(string.Join(
",", stack.ToArray()));
// Use TryPeek to get top of the stack.
if (stack.TryPeek(out int resultPeek))
{
Console.WriteLine(
"TryPeek result:" + resultPeek);
}
// Use TryPop to get and remove top of the stack.
if (stack.TryPop(out int resultPop))
{
Console.WriteLine(
"TryPop result:" + resultPop);
}
Console.WriteLine(string.Join(
",", stack.ToArray()));
}
}
0,10,50
2000,0,10,50
TryPeek result:2000
TryPop result:2000
0,10,50