C# Queue Tutorial with Examples

C# Queue Tutorial with Examples

Today, We want to share with you C# Queue Tutorial with Examples.
In this post we will show you Queue in C#.Net with example, hear for C# Generic Queue Tutorial with Programming Example we will give you demo and example for implement.
In this post, we will learn about How to use C# Queue Class with an example.

Introduction: Queue in C#.Net with example

In this post, we will learn about Queue in C#.Net with example.Queue follows a first-in, first-out process.

Useful Methods of Queue Class:

Some of the most and useful methods of stack are:

READ :  AngularJS Cookies Set Get and Remove Cookies

Enqueue(object obj)

This method adds an object to the end of the Queue.

Dequeue(object obj)

This method removes and returns the object at the beginning of the Queue.

Contains(object obj)

This method determines whether an element is in the Queue.

Clear()

This methods removes all elements from the Queue.

object[] ToArray()

This methods copies the Queue to a new array.

TrimToSize()

This method sets the capacity to the actual number of elements in the Queue.

Example

Now, Create one Console Application and write below lines of codes in Visual Studio.

using System;
using System.Collections;

namespace ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Queue Demo code
            Queue _Color = new Queue();

            _Color.Enqueue("Red");   // Adding elements to queue.
            _Color.Enqueue("Green");
            _Color.Enqueue("Blue");

            Console.WriteLine("Current queue: ");
            foreach (string c in _Color) Console.Write(c + " ");

            Console.WriteLine();
            _Color.Enqueue("White");
            Console.WriteLine("Current queue: ");
            foreach (string c in _Color) Console.Write(c + " ");

            Console.WriteLine();
            Console.WriteLine("Removing some values ");
            string s = (string)_Color.Dequeue();
            Console.WriteLine("The removed value: {0}", s);            

            Console.WriteLine("Current queue: ");
            foreach (string c in _Color) Console.Write(c + " ");

            Console.ReadKey();
        }
    }
}

Read :

READ :  Angular 6 CRUD Operations Application Tutorials

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about C# Queue Collection Example.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Add a Comment

Your email address will not be published. Required fields are marked *