Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Queue.Clone() Method in C#
The Queue.Clone() method in C# is used to create a shallow copy of the Queue. This method returns a new Queue object that contains references to the same elements as the original Queue, but the Queue structure itself is independent.
Syntax
Following is the syntax for the Queue.Clone() method −
public virtual object Clone();
Return Value
The method returns an object that represents a shallow copy of the Queue. You need to cast it back to Queue type to use it as a Queue.
Understanding Shallow Copy
A shallow copy means that the new Queue contains references to the same objects as the original Queue. If the Queue contains reference types, both queues will point to the same objects in memory.
Using Queue.Clone() with String Elements
Example
using System;
using System.Collections;
public class Demo {
public static void Main(string[] args) {
Queue queue = new Queue();
queue.Enqueue("One");
queue.Enqueue("Two");
queue.Enqueue("Three");
queue.Enqueue("Four");
queue.Enqueue("Five");
queue.Enqueue("Six");
queue.Enqueue("Seven");
queue.Enqueue("Eight");
Console.WriteLine("Original Queue...");
foreach(string str in queue) {
Console.WriteLine(str);
}
Queue queue2 = (Queue)queue.Clone();
Console.WriteLine("\nCloned Queue...");
foreach(string str in queue2) {
Console.WriteLine(str);
}
Console.WriteLine("\nOriginal Count: " + queue.Count);
Console.WriteLine("Cloned Count: " + queue2.Count);
}
}
The output of the above code is −
Original Queue... One Two Three Four Five Six Seven Eight Cloned Queue... One Two Three Four Five Six Seven Eight Original Count: 8 Cloned Count: 8
Using Queue.Clone() with Integer Elements
Example
using System;
using System.Collections;
public class Demo {
public static void Main(string[] args) {
Queue queue = new Queue();
queue.Enqueue(10);
queue.Enqueue(20);
queue.Enqueue(30);
queue.Enqueue(40);
queue.Enqueue(50);
Console.WriteLine("Original Queue...");
foreach(int num in queue) {
Console.WriteLine(num);
}
Queue queue2 = (Queue)queue.Clone();
Console.WriteLine("\nCloned Queue...");
foreach(int num in queue2) {
Console.WriteLine(num);
}
// Modify original queue
queue.Dequeue();
Console.WriteLine("\nAfter dequeuing from original:");
Console.WriteLine("Original Count: " + queue.Count);
Console.WriteLine("Cloned Count: " + queue2.Count);
}
}
The output of the above code is −
Original Queue... 10 20 30 40 50 Cloned Queue... 10 20 30 40 50 After dequeuing from original: Original Count: 4 Cloned Count: 5
Key Points
-
The
Clone()method creates a shallow copy − both queues share references to the same objects. -
The returned object must be cast back to Queue type.
-
Modifying the queue structure (adding/removing elements) affects only one queue.
-
For value types like integers, the behavior appears like a deep copy since values are copied.
Conclusion
The Queue.Clone() method provides an efficient way to create a shallow copy of a Queue in C#. The cloned queue is independent in terms of structure but shares element references with the original queue, making it useful for scenarios where you need to preserve the original queue while working with a copy.
