Open In App

Stack.Clone() Method in C#

Last Updated : 05 Feb, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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.




// 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);
    }
}


Output:

Java
C++
C

Example 2:




// 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);
    }
}


Output:

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

Reference:



Previous Article
Next Article

Similar Reads

C# | Clone() Method
In C#, Clone() is a String method. It is used to clone the string object, which returns another copy of that data. In other words, it returns a reference to this instance of String. The return value will be only another view of the same data. Clone method called directly on current String instance. This method will not take any parameters. Syntax:
1 min read
Queue.Clone() Method in C#
This method is used to create a shallow copy of the Queue. It just creates a copy of the Queue. The copy will have a reference to a clone of the internal elements but not a reference to the original elements. Syntax: public virtual object Clone (); Return Value: The method returns an Object which is just the shallow copy of the Queue. Example 1: Le
2 min read
C# | CharEnumerator.Clone() Method
CharEnumerator.Clone Method is used to create a copy of the current CharEnumerator object. This is useful for saving the current state while iterating through a String object. Syntax: public object Clone (); Return Value: This method returns an Object which is a copy of the current CharEnumerator object with the current state of the CharEnumerator
2 min read
Stack.Peek Method in C#
This method(comes under System.Collections namespace) is used to return the object at the top of the Stack without removing it. This method is similar to the Pop method, but Peek does not modify the Stack. Syntax: public virtual object Peek (); Return Value: It returns the Object at the top of the Stack. Exception: Calling Peek() method on empty st
2 min read
C# | Stack<T>.TrimExcess Method with Examples
Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access to items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item. Stack<T>.TrimExcess Method is used to set the capacity to the actual number of elements in the Queue<T
2 min read
Stack.Contains() Method in C#
This method(comes under System.Collections namespace) is used to check whether a specified element is present is Stack or not. Internally this method checks for equality by calling the Object.Equals method. Also, it performs a linear search, therefore, this method is an O(n) operation, where n is Count. Syntax: public virtual bool Contains (object
2 min read
Stack.Clear Method in C#
This method(comes under System.Collections namespace) is used to remove all the objects from the Stack. This method will set the Count of Stack to zero, and references to other objects from elements of the collection are also removed. This method is an O(n) operation, where n is Count. Syntax: public virtual void Clear (); Below programs illustrate
2 min read
Stack.CopyTo() Method in C#
This method(comes under System.Collections namespace) is used to copy the Stack to an existing 1-D Array which starts from the specified array index. The elements are copied onto the array in last-in-first-out (LIFO) order, similar to the order of the elements returned by a succession of calls to Pop. This method is an O(n) operation, where n is Co
2 min read
Stack.GetEnumerator Method in C#
This method returns an IEnumerator that iterates through the Stack. And it comes under the System.Collections namespace. Syntax: public virtual System.Collections.IEnumerator GetEnumerator (); Below programs illustrate the use of above-discussed method: Example 1: // C# program to illustrate the // Stack.GetEnumerator Method using System; using Sys
2 min read
Stack.Push() Method in C#
This method(comes under System.Collections namespace) is used to inserts an object at the top of the Stack. If the Count already equals the capacity, the capacity of the Stack is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added. If Count is less than th
2 min read
Article Tags :