Which is the most correct pattern for using Stack.Pop? There at least 2 patterns of using Stack.Pop




private Stack&lt;T&gt; availableResources;
...
if (availableResources.Count &gt; 0) 
{
  resource = availableResources.Pop();
  ...
}



private Stack&lt;T&gt; availableResources;
...
try 
{
  resource = availableResources.Pop();
  ...
}
catch (InvalidOperationException e)
{
  ...
}



I'd like to ask people, which of 2 is preferable? If possible, argument your answer.
Program defensively and use option 1.

Since the Stack class provides the ability to check whether it contains anything before trying to Pop a value from it, you should do the check and avoid using the exception for flow control.

If you want to avoid doing the if check throughout your application, you could create a TryPop extension.

public static class StackExtensions
{
    public static bool TryPop&lt;T&gt;(this Stack&lt;T&gt; stack, out T value)
    {
        if (stack.Count &gt; 0)
        {
            value = stack.Pop();
            return true;
        }

        value = default(T);
        return false;
    }
}


It could be used in the following way:

T resource;
if (availableResources.TryPop(out resource)) 
{
    // use `resource` for something
}


It's worth noting that this approach is only suitable if the Stack is not accessed concurrently! - If it is, and you are using .NET 4.0 onwards then you should use ConcurrentStack, otherwise create your own concurrent stack which encapsulates a Stack and manages access to it with locks.