LogIn
I don't have account.

Queue<T> in C#

DevSniper
147 Views

Namespace :- System.Collections.Generic
Assembly :- System.Collections.dll
Signature :-
public class Queue<T> : 
    System.Collections.Generic.IEnumerable<T>,
    System.Collections.Generic.IReadOnlyCollection<T>,
    System.Collections.ICollection
  • Queue operates on the first-in-first-out (FIFO) principle.
  • You should use Queue where you need to access the information/data in order that it is stored.
  • Queue can store duplicate elements.
  • Queue can store null.
  • Queue is not Thread Safe.
Queue<T> Constructors
  1. public Queue ();
  2. public Queue (System.Collections.Generic.IEnumerable<T> collection);
  3. public Queue (int capacity);
Queue<T> Properties
  • Count :- public int Count { get; }
    Gets the number of elements that are actually present in the Queue<T>.
Creating and adding element in Queue<T>
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        var queue = new Queue<string>();
        queue.Enqueue("Python");
        queue.Enqueue("Java");
        queue.Enqueue("C#");
        queue.Enqueue("React");
        queue.Enqueue("C++");
        Console.WriteLine("Peek element :- "+ queue.Peek());
        Console.WriteLine("Dequeue :- "+ queue.Dequeue());
        Console.WriteLine("After Dequeue, Peek element :- " + queue.Peek());
    }
}
Peek element :- Python
Dequeue :- Python
After Dequeue, Peek element :- Java