Home » C#.Net

Queue.Clone() method with example in C#

C# Queue.Clone() method: Here, we are going to learn about the Clone() method of Queue class in C#.
Submitted by IncludeHelp, on March 30, 2019

C# Queue.Clone() method

Queue.Clone() method is used to create a shallow copy of a Queue, in other words we can say it can be used to create a clone to a Queue.

Syntax:

    Object Queue.Clone();

Parameters: None

Return value: Object – it returns an object containing copy 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 create a copy to the Queue using Queue.Clone() method

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

Reference: Queue.Clone Method



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.