Mono Class Library: System.Array Overview | Members

System.Array.Clone Method

Returns a object that is a copy of the current instance. [Edit]

public object Clone ()

Returns

A object that is a copy of the current instance. [Edit]

Remarks

Note: This method is implemented to support the ICloneable interface.

Operation
Each of the elements of the current instance is copied to the clone. If the elements are reference types, the references are copied. If the elements are value-types, the values are copied. The clone is of the same type as the current instance.

As described above.

Note to Inheritors
Override this method to return a clone of an array.

Usage
Use this method to obtain the clone of an array.

[Edit]

Example

This example demonstrates the Array.Clone method.

C# Example
using System;
public class ArrayCloneExample {
  public static void Main() {
    int[] intAryOrig = { 3, 4, 5 };
    //must explicitly convert clones object into an array
    int[] intAryClone = (int[]) intAryOrig.Clone();
    Console.Write( "The elements of the first  array are: " );
    foreach( int i in intAryOrig )
      Console.Write( "{0,3}", i );
    Console.WriteLine();
    Console.Write( "The elements of the cloned array are: " );
    foreach( int i in intAryClone )
      Console.Write( "{0,3}", i );
    Console.WriteLine();
    //Clear the values of the original array.
    Array.Clear( intAryOrig, 0, 3 );
    Console.WriteLine( "After clearing the first array," );
    Console.Write( "The elements of the first  array are: " );
    foreach( int i in intAryOrig )
      Console.Write( "{0,3}", i );
    Console.WriteLine();
    Console.Write( "The elements of the cloned array are: " );
    foreach( int i in intAryClone )
      Console.Write( "{0,3}", i );
  }
}
   

The output is

The elements of the first array are: 3 4 5
The elements of the cloned array are: 3 4 5
After clearing the first array,
The elements of the first array are: 0 0 0
The elements of the cloned array are: 3 4 5

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0