- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# - Switch Expressions
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# - Foreach Loop
- C# - Goto Statement
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - LINQ
- C# - IEnumerable vs IEnumerator
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Tasks and Parallel Programming
- C# - Multithreading
- C# - Extension Methods
C# Stack - Clone() Method
The C# stack Clone() method is used to create a shallow copy of the stack.
A shallow copy makes a new object that reference to the same data as the original object, but it doesn't reference directly to the original data. This method tales O(n) operation, where n is the count.
It is important to note that the generic stack<T> class does not support the Clone() method. However, the non-generic stack from system.collection supports Clone() because it implements the ICloneable interface.
Syntax
Following is the syntax of the C# stack Clone() method −
public virtual object? Clone();
Parameters
This method does not accepts any parameters.
Return value
This method returns a shallow copy of the stack
Example 1: Create Shallow Copy of Stack
Following is the basic example of the Clone() method to return the top element from the stack −
using System;
using System.Collections;
class Program {
static void Main() {
Stack originalStack = new Stack();
// Pushing elements onto the stack
originalStack.Push("Hello");
originalStack.Push("tutorialspoint");
originalStack.Push(50);
// Cloning the stack
Stack clonedStack = (Stack)originalStack.Clone();
// Displaying cloned stack elements
Console.WriteLine("Cloned Stack Elements:");
foreach (var item in clonedStack) {
Console.WriteLine(item);
}
}
}
Output
Following is the output −
Cloned Stack Elements: 50 tutorialspoint Hello
Example 2: Cloning a Stack
Here, in this example we demonstrate how to use the Clone() method of "System.Collections.Stack" to create a shallow copy of a stack −
using System;
using System.Collections;
class Example {
static void Main() {
Stack originalStack = new Stack();
originalStack.Push("Apple");
originalStack.Push("Banana");
originalStack.Push("Cherry");
// Cloning the stack
Stack clonedStack = (Stack)originalStack.Clone();
// Displaying the original stack
Console.WriteLine("Original Stack Elements:");
foreach (var item in originalStack) {
Console.WriteLine(item);
}
// Displaying the cloned stack
Console.WriteLine("\nCloned Stack Elements:");
foreach (var item in clonedStack) {
Console.WriteLine(item);
}
// Checking if both stacks have the same count
Console.WriteLine("\nAre both stacks equal in size? " + (originalStack.Count == clonedStack.Count));
}
}
Output
Following is the output −
Original Stack Elements: Cherry Banana Apple Cloned Stack Elements: Cherry Banana Apple Are both stacks equal in size? True
Example 3: Clone After Removing one Object
The example below demonstrates the Clone() method, which creates a shallow copy of the stack after removing an object from it −
using System;
using System.Collections;
class Example {
static void Main() {
Stack originalStack = new Stack();
originalStack.Push("Apple");
originalStack.Push("Banana");
originalStack.Push("Cherry");
// Displaying the original stack
// before popped
Console.WriteLine("Original Stack Elements:");
foreach (var item in originalStack) {
Console.WriteLine(item);
}
Console.WriteLine("");
// Pop an element
Console.Write("Popped Element: "+ originalStack.Pop() + "\n");
// Cloning the stack
Stack clonedStack = (Stack)originalStack.Clone();
// Displaying the cloned stack
Console.WriteLine("\nCloned Stack Elements:");
foreach (var item in clonedStack) {
Console.WriteLine(item);
}
}
}
Output
Following is the output −
Original Stack Elements: Cherry Banana Apple Popped Element: Cherry Cloned Stack Elements: Banana Apple