Home  |  Delphi .net Home  |  System.Collections.Stack  |  CopyTo Method
  
CopyTo  
Method  
Copies elements from the Stack to a single dimension array
Stack Class
System.Collections NameSpace
CF1.  Procedure CopyTo ( TargetArray : System.Array; TargetIndex : Integer ) ;
CF : Methods with this mark are Compact Framework Compatible
Description
The CopyTo method takes all elements from the current Stack and overlays these values onto elements of the TargetArray.
 
The overlay starts at the TargetIndex value.
 
Important : the TargetArray must be created in advance - the CopyTo method overlays - it does not build. Also, the target array must have at least the same capacity as the number of elements being overlaid (copied).
 
Important : the TargetArray data type must be able to accomodate the Stack data types in the copy process. If the CopyTo method cannot cast to the target array, then an exception is thrown.
 
Just like Clone, CopyTo performs a shallow copy. When the current Stack holds reference (non primitive) data types, the target array 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.
Microsoft MSDN Links
System.Collections
System.Collections.Stack
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
Copying the whole Stack onto the middle of an Array
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

var
  MyStack    : System.Collections.Stack;
  MyArray    : System.Array;
  i          : Integer;

begin
  // Create our stack
  MyStack := Stack.Create;

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

  // Create the target array
  MyArray := System.Array.CreateInstance(TypeOf(String), 7);

  // And fill it
  MyArray.SetValue('0.', 0);
  MyArray.SetValue('1.', 1);
  MyArray.SetValue('2.', 2);
  MyArray.SetValue('3.', 3);
  MyArray.SetValue('4.', 4);
  MyArray.SetValue('5.', 5);
  MyArray.SetValue('6.', 6);

  // Copy the stack to the middle of the array
  MyStack.CopyTo(MyArray, 2);

  // Display the array
  Console.WriteLine('MyArray now holds :');
  Console.WriteLine;
  for i := 0 to MyArray.Length-1 do
    Console.WriteLine(MyArray.GetValue(i).toString);

  Console.Readline;
end.
   MyArray now holds :
  
   0.
   1.
   Third
   Second
   First
   5.
   6.
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author