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
csharp_stack.htm
Advertisements