4

In the ConcurrentQeueue<> class, and extra method TryDequeue() is defined. But, as it implements IProducerConsumerCollection<>, it also has a TryTake() method. According to the docs, they both do the same thing:

TryDequeue:

Tries to remove and return the object at the beginning of the concurrent queue.

TryTake:

For ConcurrentQueue, this operation will attempt to remove the object from the beginning of the ConcurrentQueue.

Why bother with implementing that TryDequeue method?

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195

1 Answers1

6

What is the difference between TryDequeue and TryTake in a ConcurrentQueue<>

According to available source code there is no difference as TryTake invokes TryDequeue

/// <summary>
/// Attempts to remove and return an object from the <see cref="Concurrent.IProducerConsumerCollection{T}"/>.
/// </summary>
/// <param name="item">
/// When this method returns, if the operation was successful, <paramref name="item"/> contains the
/// object removed. If no object was available to be removed, the value is unspecified.
/// </param>
/// <returns>true if an element was removed and returned successfully; otherwise, false.</returns>
/// <remarks>For <see cref="ConcurrentQueue{T}"/>, this operation will attempt to remove the object
/// from the beginning of the <see cref="ConcurrentQueue{T}"/>.
/// </remarks>
bool IProducerConsumerCollection<T>.TryTake([MaybeNullWhen(false)] out T item) => TryDequeue(out item);

Souce: https://source.dot.net/#System.Private.CoreLib/ConcurrentQueue.cs,201

Why bother with implementing that TryDequeue method?

TryDequeue follows expected name conventions associated with queues and is local to ConcurrentQueue<>. As well, TryTake follows naming convention normally associated with producer/consumer pattern.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Thanks, I reckon `TryAdd` calls `Enqueue` ? – Bart Friederichs Oct 08 '21 at 13:04
  • @BartFriederichs that would accurate https://source.dot.net/#System.Private.CoreLib/ConcurrentQueue.cs,184 – Nkosi Oct 08 '21 at 13:05
  • 1
    `IProducerConsumerCollection<>.TryTake` is explicitly implemented in `ConcurrentQueue<>`. Then it isn't possible to directly call `ConcurrentQueue<>.TryTake`. When you manipulate `ConcurrentQueue` you can use `TryDequeue` and when you manipulate `IProducerConsumerCollection` you can use `TryTake` – vernou Oct 08 '21 at 13:05
  • @vernou you are correct. You would first need to cast to the interface to access that member – Nkosi Oct 08 '21 at 13:07
  • This is all great, thanks. I am using the `IProducerConsumerCollection` in my class, so that I can inject it as a `ConcurrentQueue` for my unit tests. – Bart Friederichs Oct 08 '21 at 13:21