Introduction
The SystemCollectionsGenericQueue is a versatile queue implementation available in .NET Core and .NET Framework. It provides efficient and reliable queuing functionality, allowing you to manage asynchronous operations and data streams effectively.
Properties
- size: The number of elements in the queue.
- capacity: The maximum number of elements the queue can hold.
- lock: A lock for thread-safe queue operations.
- is_initialized: Boolean indicating if the queue is initialized.
- dequeue: Returns the element removed from the queue.
- enqueue: Adds an element to the queue.
- isEmpty: Returns true if the queue is empty, false otherwise.
Methods
The queue provides methods for:
- GetSize(): Returns the current queue size.
- GetCapacity(): Returns the current queue's capacity.
- SetLock(): Sets the lock for the queue.
- Enqueue(T item): Adds an item to the queue.
- Dequeue(): Removes and returns the element at the front of the queue.
- isEmpty(): Checks if the queue is empty.
Example
Here's a simple example demonstrating the queue's capabilities:
using System;
using System.Collections.Generic;
public class SystemCollectionsGenericQueue
{
private readonly List _items = new List();
public bool IsEmpty() { return _items.Count == 0; }
public T Get(int index) {
if (index < 0 || index >= _items.Count) {
throw new IndexOutOfRangeException("Index out of range");
}
return _items[index];
}
public void Enqueue(T item) {
_items.Add(item);
}
public T Dequeue() {
if (_items.Count == 0) {
throw new InvalidOperationException("Queue is empty");
}
return _items.RemoveFirst();
}
}