Stack.Clone() Method in C#

This method is used to create a shallow copy of the Stack. It just creates a copy of the Stack. The copy will have a reference to a clone of the internal data array but not a reference to the original internal data array.

Syntax: public virtual object Clone ();

Return Value: The method returns an Object which is just the shallow copy of the Stack.

Example 1: Let’s see an example without using a Clone() method and directly copying a Stack using assignment operator ‘=’. In the below code, we can see even if we Pop() elements from myStack2, contents of myStack is also changed. This is because ‘=’ just assigns the reference of myStack to myStack2 and does not create any new Stack. But Clone() creates a new Stack.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to Copy a Stack using 
// the assignment operator
using System;
using System.Collections;
  
class GFG {
  
    // Main Method
    public static void Main(string[] args)
    {
  
        Stack myStack = new Stack();
        myStack.Push("C");
        myStack.Push("C++");
        myStack.Push("Java");
        myStack.Push("C#");
  
        // Creating a copy using the
        // assignment operator.
        Stack myStack2 = myStack; 
  
        // Removing top most element
        myStack2.Pop(); 
        PrintValues(myStack);
    }
  
    public static void PrintValues(IEnumerable myCollection)
    {
        // This method prints all
        // the elements in the Stack.
        foreach(Object obj in myCollection)
            Console.WriteLine(obj);
    }
}

chevron_right


Output:

Java
C++
C

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the use 
// of Stack.Clone() Method 
using System;
using System.Collections;
  
class MainClass {
  
    // Main Method
    public static void Main(string[] args)
    {
  
        Stack myStack = new Stack();
        myStack.Push("1st Element");
        myStack.Push("2nd Element");
        myStack.Push("3rd Element");
        myStack.Push("4th Element");
  
        // Creating copy using Clone() method
        Stack myStack3 = (Stack)myStack.Clone(); 
      
        // Removing top most element
        myStack3.Pop(); 
        PrintValues(myStack);
    }
  
    public static void PrintValues(IEnumerable myCollection)
    {
        // This method prints all the
        // elements in the Stack.
        foreach(Object obj in myCollection)
            Console.WriteLine(obj);
    }
}

chevron_right


Output:

4th Element
3rd Element
2nd Element
1st Element

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.