Home  |  Delphi .net Home  |  System.Collections.Stack  |  Clone Method
  
Clone  
Method  
Make a new Stack that is a clone of the current Stack
Stack Class
System.Collections NameSpace
CF1.  Function Clone : Object;
CF : Methods with this mark are Compact Framework Compatible
Description
A new Stack is created with the same number of elements, and entries as the current Stack.
 
When the current stack holds reference (non primitive) data types, the cloned stack element values still refer to the same objects that current stack elements refer to. This is what is referred to as a shallow copy. A deep copy would create new versions of the referred objects.
Notes
You are obliged to cast the resultant Object to a Stack before assignment to a Stack variable.
Microsoft MSDN Links
System.Collections
System.Collections.Stack
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

var
  Source, Target : System.Collections.Stack;
  Enumerator     : IEnumerator;

begin
  // Create our stacks
  Source := Stack.Create;
  Target := Stack.Create;

  // Add entries to the source stack
  Source.Push('First');
  Source.Push('Second');
  Source.Push('Third');

  // Display the stack
  Console.WriteLine('Source stack :');
  Console.WriteLine;
  Enumerator := Source.GetEnumerator;
  while Enumerator.MoveNext do
    Console.WriteLine(Enumerator.Current.ToString);

  // Clone the stack!
  Target := Stack(Source.Clone);

  // Display the target
  Console.WriteLine;
  Console.WriteLine('Cloned stack :');
  Console.WriteLine;
  Enumerator := Target.GetEnumerator;
  while Enumerator.MoveNext do
    Console.WriteLine(Enumerator.Current.ToString);

  Console.Readline;
end.
   Source stack :
  
   Third
   Second
   First
  
   Cloned stack :
  
   Third
   Second
   First
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author