I am writing a type of Queue implementation that has a
TryDequeue method that uses a pattern similar to various .NET
TryParse methods, where I return a boolean value if the action succeeded, and use an
out parameter to return the actual dequeued value.
public bool TryDequeue(out Message message) => _innerQueue.TryDequeue(out message);
Now, I like to avoid
out params whenever I can. C# 7 gives us out variable delcarations to make working with them easier, but I still consider out params more of a necessary evil than a useful tool.
The behaviour I want from this method is as follows:
- If there is an item to dequeue, return it.
- If there are no items to dequeue (the queue is empty), provide the caller with enough information to act appropriately.
- Don't just return a null item if there are no items left.
- Don't throw an exception if trying to dequeue from an empty queue.
Right now, a caller of this method would almost always use a pattern like the following (using C#7 out variable syntax):
if (myMessageQueue.TryDequeue(out Message dequeued)) MyMessagingClass.SendMessage(dequeued) else Console.WriteLine("No messages!"); // do other stuff
Which isn't the worst, all told. But I can't help but feel there might be nicer ways to do this (I'm totally willing to concede that there might not be). I hate how the caller has to break up it's flow with a conditional when all it wants to is get a value if one exists.
What are some other patterns that exist to accomplish this same "try" behaviour?
For context, this method may potentially be called in VB projects, so bonus points for something that works nice in both. This fact should carry very little weight, though.