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
Stack.Clone() Method in C#
The Stack.Clone() method in C# creates a shallow copy of the Stack. This method returns a new Stack object with the same elements as the original, but both stacks remain independent for basic operations like Push and Pop.
Syntax
Following is the syntax for the Stack.Clone() method −
public virtual object Clone();
Return Value
The method returns an object that represents a shallow copy of the Stack. You need to cast it back to a Stack type to use it as a Stack.
Understanding Shallow Copy
A shallow copy means that the new Stack contains references to the same objects as the original Stack. For value types like integers and strings, this creates independent copies. However, for reference types, both stacks would reference the same objects in memory.
Using Stack.Clone() with Integers
Example
using System;
using System.Collections;
public class Demo {
public static void Main() {
Stack stack = new Stack();
stack.Push(150);
stack.Push(300);
stack.Push(500);
stack.Push(750);
stack.Push(1000);
Console.WriteLine("Original Stack elements:");
foreach(int val in stack) {
Console.WriteLine(val);
}
Console.WriteLine("Count = " + stack.Count);
Stack clonedStack = (Stack)stack.Clone();
Console.WriteLine("\nCloned Stack elements:");
foreach(int val in clonedStack) {
Console.WriteLine(val);
}
Console.WriteLine("Cloned Count = " + clonedStack.Count);
// Modify original stack
stack.Push(2000);
Console.WriteLine("\nAfter adding 2000 to original:");
Console.WriteLine("Original Count = " + stack.Count);
Console.WriteLine("Cloned Count = " + clonedStack.Count);
}
}
The output of the above code is −
Original Stack elements: 1000 750 500 300 150 Count = 5 Cloned Stack elements: 1000 750 500 300 150 Cloned Count = 5 After adding 2000 to original: Original Count = 6 Cloned Count = 5
Using Stack.Clone() with Strings
Example
using System;
using System.Collections;
public class Demo {
public static void Main() {
Stack stack = new Stack();
stack.Push("Laptop");
stack.Push("Monitor");
stack.Push("Keyboard");
stack.Push("Mouse");
Console.WriteLine("Original Stack:");
foreach(string val in stack) {
Console.WriteLine(val);
}
Stack clonedStack = (Stack)stack.Clone();
Console.WriteLine("\nCloned Stack:");
foreach(string val in clonedStack) {
Console.WriteLine(val);
}
// Test independence
clonedStack.Pop();
Console.WriteLine("\nAfter Pop() on cloned stack:");
Console.WriteLine("Original Count: " + stack.Count);
Console.WriteLine("Cloned Count: " + clonedStack.Count);
}
}
The output of the above code is −
Original Stack: Mouse Keyboard Monitor Laptop Cloned Stack: Mouse Keyboard Monitor Laptop After Pop() on cloned stack: Original Count: 4 Cloned Count: 3
Key Points
-
The
Clone()method returns anobject, so you must cast it toStack. -
The cloned Stack is independent − changes to one Stack don't affect the other.
-
For value types and immutable reference types like strings, shallow copy works like deep copy.
-
The elements maintain the same order (LIFO) in both stacks.
Conclusion
The Stack.Clone() method creates an independent shallow copy of a Stack with the same elements in the same order. This is useful when you need to preserve the original Stack while performing operations on a duplicate copy.
