ProgrammingDesktop Application Developer in Visual Basic

How are Queue and Stack collections implemented and used in Visual Basic, when to choose each structure, and what should be considered when working with queues and stacks?

Pass interviews with Hintsage AI assistant

Answer.

History of the question:

Queue and Stack collections are basic data structures that implement FIFO (First-In-First-Out) and LIFO (Last-In-First-Out) principles, respectively. They have been actively used since the classic VB6 era, and with the advent of VB.NET, these structures became standard classes. Queue is useful for scenarios where elements need to be processed in the order they were received (e.g., task queue), while Stack is used when the most recently added element needs to be processed before others (e.g., call stack or undo actions).

Problem:

Often, the wrong structure is mistakenly chosen for a specific task, or they are used unsafely in multithreaded scenarios. There are also errors related to extracting from an empty stack or queue, which can cause exceptions.

Solution:

In Visual Basic, there are corresponding classes for working with these structures:

  • System.Collections.Queue
  • System.Collections.Stack

Example of how to work with a queue and a stack:

' Working with a queue Dim q As New Queue() q.Enqueue("First") q.Enqueue("Second") Dim item = q.Dequeue() ' item = "First" ' Working with a stack Dim s As New Stack() s.Push("A") s.Push("B") Dim top = s.Pop() ' top = "B"

Key features:

  • Queue provides FIFO order, Stack provides LIFO.
  • Use Peek() to access the first element, Dequeue()/Pop() to remove.
  • It is necessary to check the Count property to prevent errors when extracting from an empty collection.

Trick questions.

1. Can you modify a Queue or Stack while iterating over it using For Each?

No, modifying (adding or removing elements) a collection during iteration with For Each will lead to an InvalidOperationException.

Example code:

Dim q As New Queue() q.Enqueue(1) q.Enqueue(2) For Each elem In q q.Enqueue(3) ' will raise an exception during iteration Next

2. What will the Peek() method return for an empty Queue or Stack?

Peek throws an InvalidOperationException if the collection is empty, rather than returning a default value.

Example code:

Dim st As New Stack() Dim first As Object = st.Peek() ' Exception!

3. What is the difference between Queue(Of T) and Queue, and why should generic collections be preferred?

Queue(Of T) is the generic version of Queue, it is type-safe and avoids boxing/unboxing. It is always better to choose it when working with known types.

Dim numbers As New Queue(Of Integer)() numbers.Enqueue(5) ' Only Integer

Typical mistakes and anti-patterns

  • Attempting to extract an element from an empty Queue/Stack without checking Count.
  • Using non-generic collections when there is a queue or stack with a known data type (loss of type safety, casting errors).
  • Modifying the collection during iteration.

Real-life example

Negative case

In an application where a print queue for documents is implemented, a developer stores print jobs in a Stack instead of a Queue. As a result, documents are printed in reverse order (the last added is processed first).

Pros:

  • Simple implementation, all standard operations are available.

Cons:

  • Violates the logic of operation — the user expects documents to be processed in the order they were added but receives the opposite behavior.
  • The sequence of events gets confusing and negative feedback arises from users.

Positive case

The same project changes from Stack to Queue for storing print jobs. Now documents are printed in order from left to right, as the user expects.

Pros:

  • Behavior matches user expectations.
  • Support and testing are simplified.

Cons:

  • If concurrent access to the queue is not managed correctly, races may occur (can be resolved with thread-safe collections or synchronization).