Queue.Clone() method with example in C#

C# Queue.Clone() technique: Here, we will find out about the Clone() strategy for Queue class in C#.

C# Queue.Clone() technique

Queue.Clone() technique is utilized to make a shallow duplicate of a Queue, at the end of the day we can say it very well may be utilized to make a clone to a Queue.

Syntax:

Queue.Clone() method with example in C#

Parameters: None

Return value: Object – it restores an article containing duplicate to the Queue.

Example:

    declare and initialize a Queue:
    Queue que = new Queue();   

    insertting elements:
    que.Enqueue(100);
    que.Enqueue(200);
    que.Enqueue(300);
    que.Enqueue(400);
    que.Enqueue(500);
    
    copying the Queue:
    Queue que1 = (Queue)que.Clone();

    Output:
    que elements...
    100 200 300 400 500
    que1 elements...
    100 200 300 400 500

C# example to make a duplicate to the Queue utilizing Queue.Clone() strategy

using System;
using System.Text;
using System.Collections;

namespace Test
{
    class Program
    {
        //function to print queue elements
        static void printQueue(Queue q)
        {
            foreach (Object obj in q)
            {
                Console.Write(obj + " ");
            }
            Console.WriteLine();
        }

        static void Main(string[] args)
        {
            //declare and initialize a Queue
            Queue que = new Queue();

            //insertting elements
            que.Enqueue(100);
            que.Enqueue(200);
            que.Enqueue(300);
            que.Enqueue(400);
            que.Enqueue(500);

            //copying the Queue
            Queue que1 = (Queue)que.Clone();

            //printing Queues 
            Console.WriteLine("que elements...");
            printQueue(que);
            Console.WriteLine("que1 elements...");
            printQueue(que);

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output

que elements...
100 200 300 400 500
que1 elements...
100 200 300 400 500

Leave a Comment

error: Alert: Content is protected!!