Array.Copy Methode
Definition
Überlädt
Copy(Array, Int64, Array, Int64, Int64) |
Kopiert einen beim angegebenen Quellindex beginnenden Elementbereich aus einem Array und fügt ihn ab dem angegebenen Zielindex in ein anderes Array ein.Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. Die Länge und die Indizes werden als 64-Bit-Ganzzahlen angegeben.The length and the indexes are specified as 64-bit integers. |
Copy(Array, Int32, Array, Int32, Int32) |
Kopiert einen beim angegebenen Quellindex beginnenden Elementbereich aus einem Array und fügt ihn ab dem angegebenen Zielindex in ein anderes Array ein.Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. Die Länge und die Indizes werden als 32-Bit-Ganzzahlen angegeben.The length and the indexes are specified as 32-bit integers. |
Copy(Array, Array, Int64) |
Kopiert einen mit dem ersten Element beginnenden Elementbereich eines Array und fügt ihn ab dem ersten Element in ein anderes Array ein.Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. Die Länge wird als 64-Bit-Ganzzahl angegeben.The length is specified as a 64-bit integer. |
Copy(Array, Array, Int32) |
Kopiert einen mit dem ersten Element beginnenden Elementbereich eines Array und fügt ihn ab dem ersten Element in ein anderes Array ein.Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. Die Länge wird als 32-Bit-Ganzzahl angegeben.The length is specified as a 32-bit integer. |
Copy(Array, Int64, Array, Int64, Int64)
Kopiert einen beim angegebenen Quellindex beginnenden Elementbereich aus einem Array und fügt ihn ab dem angegebenen Zielindex in ein anderes Array ein.Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. Die Länge und die Indizes werden als 64-Bit-Ganzzahlen angegeben.The length and the indexes are specified as 64-bit integers.
public:
static void Copy(Array ^ sourceArray, long sourceIndex, Array ^ destinationArray, long destinationIndex, long length);
public static void Copy (Array sourceArray, long sourceIndex, Array destinationArray, long destinationIndex, long length);
static member Copy : Array * int64 * Array * int64 * int64 -> unit
Public Shared Sub Copy (sourceArray As Array, sourceIndex As Long, destinationArray As Array, destinationIndex As Long, length As Long)
Parameter
- sourceArray
- Array
Das Array, das die zu kopierenden Daten enthält.The Array that contains the data to copy.
- sourceIndex
- Int64
Eine 64-Bit-Ganzzahl, die den Index im sourceArray
angibt, ab dem kopiert werden soll.A 64-bit integer that represents the index in the sourceArray
at which copying begins.
- destinationIndex
- Int64
Eine 64-Bit-Ganzzahl, die den Index im destinationArray
angibt, ab dem gespeichert werden soll.A 64-bit integer that represents the index in the destinationArray
at which storing begins.
- length
- Int64
Eine 64-Bit-Ganzzahl, die die Anzahl der zu kopierenden Elemente darstellt.A 64-bit integer that represents the number of elements to copy. Die Ganzzahl muss zwischen 0 (null) und einschließlich MaxValue liegen.The integer must be between zero and MaxValue, inclusive.
Ausnahmen
sourceArray
ist null
.sourceArray
is null
.
- oder --or-
destinationArray
ist null
.destinationArray
is null
.
sourceArray
und destinationArray
sind von unterschiedlichem Rang.sourceArray
and destinationArray
have different ranks.
sourceArray
und destinationArray
weisen inkompatible Typen auf.sourceArray
and destinationArray
are of incompatible types.
Mindestens ein Element im sourceArray
kann nicht in den Typ des destinationArray
s umgewandelt werden.At least one element in sourceArray
cannot be cast to the type of destinationArray
.
sourceIndex
liegt außerhalb des Bereichs der gültigen Indizes für das sourceArray
.sourceIndex
is outside the range of valid indexes for the sourceArray
.
- oder --or-
destinationIndex
liegt außerhalb des Bereichs der gültigen Indizes für das destinationArray
.destinationIndex
is outside the range of valid indexes for the destinationArray
.
- oder --or-
length
ist kleiner als 0 oder größer als MaxValue.length
is less than 0 or greater than MaxValue.
length
ist größer als die Anzahl der Elemente vom sourceIndex
bis zum Ende des sourceArray
s.length
is greater than the number of elements from sourceIndex
to the end of sourceArray
.
- oder --or-
length
ist größer als die Anzahl der Elemente vom destinationIndex
bis zum Ende des destinationArray
s.length
is greater than the number of elements from destinationIndex
to the end of destinationArray
.
Beispiele
Im folgenden Codebeispiel wird veranschaulicht, wie von einem Array vom Typ Object in einen anderen Array vom Typ Integer kopiert wird.The following code example shows how to copy from one Array of type Object to another Array of type integer.
using namespace System;
void PrintValues( Array^ myArr );
void main()
{
// Creates and initializes a new Array instance of type Int32.
Array^ myIntArray = Array::CreateInstance( Type::GetType( "System.Int32" ), 5 );
for ( int i = myIntArray->GetLowerBound( 0 ); i <= myIntArray->GetUpperBound( 0 ); i++ )
myIntArray->SetValue( i + 1, i );
// Creates and initializes a new Array instance of type Object.
Array^ myObjArray = Array::CreateInstance( Type::GetType( "System.Object" ), 5 );
for ( int i = myObjArray->GetLowerBound( 0 ); i <= myObjArray->GetUpperBound( 0 ); i++ )
myObjArray->SetValue( i + 26, i );
// Displays the initial values of both arrays.
Console::WriteLine( "Int32 array:" );
PrintValues( myIntArray );
Console::WriteLine( "Object array:" );
PrintValues( myObjArray );
// Copies the first element from the Int32 array to the Object array.
Array::Copy( myIntArray, myIntArray->GetLowerBound( 0 ), myObjArray, myObjArray->GetLowerBound( 0 ), 1 );
// Copies the last two elements from the Object array to the Int32 array.
Array::Copy( myObjArray, myObjArray->GetUpperBound( 0 ) - 1, myIntArray, myIntArray->GetUpperBound( 0 ) - 1, 2 );
// Displays the values of the modified arrays.
Console::WriteLine( "Int32 array - Last two elements should now be the same as Object array:" );
PrintValues( myIntArray );
Console::WriteLine( "Object array - First element should now be the same as Int32 array:" );
PrintValues( myObjArray );
}
void PrintValues( Array^ myArr )
{
System::Collections::IEnumerator^ myEnumerator = myArr->GetEnumerator();
int i = 0;
int cols = myArr->GetLength( myArr->Rank - 1 );
while ( myEnumerator->MoveNext() )
{
if ( i < cols )
{
i++;
}
else
{
Console::WriteLine();
i = 1;
}
Console::Write( "\t{0}", myEnumerator->Current );
}
Console::WriteLine();
}
/*
This code produces the following output.
Int32 array:
1 2 3 4 5
Object array:
26 27 28 29 30
Int32 array - Last two elements should now be the same as Object array:
1 2 3 29 30
Object array - First element should now be the same as Int32 array:
1 27 28 29 30
*/
using System;
public class SamplesArray {
public static void Main() {
// Creates and initializes a new Array of type Int32.
Array myIntArray=Array.CreateInstance( typeof(System.Int32), 5 );
for ( int i = myIntArray.GetLowerBound(0); i <= myIntArray.GetUpperBound(0); i++ )
myIntArray.SetValue( i+1, i );
// Creates and initializes a new Array of type Object.
Array myObjArray = Array.CreateInstance( typeof(System.Object), 5 );
for ( int i = myObjArray.GetLowerBound(0); i <= myObjArray.GetUpperBound(0); i++ )
myObjArray.SetValue( i+26, i );
// Displays the initial values of both arrays.
Console.WriteLine( "Int32 array:" );
PrintValues( myIntArray );
Console.WriteLine( "Object array:" );
PrintValues( myObjArray );
// Copies the first element from the Int32 array to the Object array.
Array.Copy( myIntArray, myIntArray.GetLowerBound(0), myObjArray, myObjArray.GetLowerBound(0), 1 );
// Copies the last two elements from the Object array to the Int32 array.
Array.Copy( myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2 );
// Displays the values of the modified arrays.
Console.WriteLine( "Int32 array - Last two elements should now be the same as Object array:" );
PrintValues( myIntArray );
Console.WriteLine( "Object array - First element should now be the same as Int32 array:" );
PrintValues( myObjArray );
}
public static void PrintValues( Array myArr ) {
System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
int i = 0;
int cols = myArr.GetLength( myArr.Rank - 1 );
while ( myEnumerator.MoveNext() ) {
if ( i < cols ) {
i++;
} else {
Console.WriteLine();
i = 1;
}
Console.Write( "\t{0}", myEnumerator.Current );
}
Console.WriteLine();
}
}
/*
This code produces the following output.
Int32 array:
1 2 3 4 5
Object array:
26 27 28 29 30
Int32 array - Last two elements should now be the same as Object array:
1 2 3 29 30
Object array - First element should now be the same as Int32 array:
1 27 28 29 30
*/
Public Class SamplesArray
Public Shared Sub Main()
' Creates and initializes a new Array of type Int32.
Dim myIntArray As Array = _
Array.CreateInstance(GetType(System.Int32), 5)
Dim i As Integer
For i = myIntArray.GetLowerBound(0) To myIntArray.GetUpperBound(0)
myIntArray.SetValue(i + 1, i)
Next i
' Creates and initializes a new Array of type Object.
Dim myObjArray As Array = _
Array.CreateInstance(GetType(System.Object), 5)
For i = myObjArray.GetLowerBound(0) To myObjArray.GetUpperBound(0)
myObjArray.SetValue(i + 26, i)
Next i
' Displays the initial values of both arrays.
Console.WriteLine("Int32 array:")
PrintValues(myIntArray)
Console.WriteLine("Object array:")
PrintValues(myObjArray)
' Copies the first element from the Int32 array to the Object array.
Array.Copy(myIntArray, myIntArray.GetLowerBound(0), myObjArray, _
myObjArray.GetLowerBound(0), 1)
' Copies the last two elements from the Object array to the Int32 array.
Array.Copy(myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, _
myIntArray.GetUpperBound(0) - 1, 2)
' Displays the values of the modified arrays.
Console.WriteLine("Int32 array - Last two elements should now be " _
+ "the same as Object array:")
PrintValues(myIntArray)
Console.WriteLine("Object array - First element should now be the " _
+ "same as Int32 array:")
PrintValues(myObjArray)
End Sub
Public Shared Sub PrintValues(myArr As Array)
Dim myEnumerator As System.Collections.IEnumerator = _
myArr.GetEnumerator()
Dim i As Integer = 0
Dim cols As Integer = myArr.GetLength((myArr.Rank - 1))
While myEnumerator.MoveNext()
If i < cols Then
i += 1
Else
Console.WriteLine()
i = 1
End If
Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
End While
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
' Int32 array:
' 1 2 3 4 5
' Object array:
' 26 27 28 29 30
' Int32 array - Last two elements should now be the same as Object array:
' 1 2 3 29 30
' Object array - First element should now be the same as Int32 array:
' 1 27 28 29 30
Hinweise
Die Parameter "sourceArray
" und "destinationArray
" müssen die gleiche Anzahl von Dimensionen aufweisen.The sourceArray
and destinationArray
parameters must have the same number of dimensions. Außerdem muss destinationArray
bereits dimensioniert sein und muss über eine ausreichende Anzahl von Elementen verfügen, beginnend mit der destinationIndex
Position, um die kopierten Daten aufzunehmen.In addition, destinationArray
must already have been dimensioned and must have a sufficient number of elements starting from the destinationIndex
position to accommodate the copied data.
Beim Kopieren zwischen mehrdimensionalen Arrays verhält sich das Array wie ein langes eindimensionales Array, bei dem die Zeilen (oder Spalten) vom Ende bis zum Ende verankert werden.When copying between multidimensional arrays, the array behaves like a long one-dimensional array, where the rows (or columns) are conceptually laid end-to-end. Wenn ein Array z. b. über drei Zeilen (oder Spalten) mit jeweils vier Elementen verfügt, werden beim Kopieren von sechs Elementen vom Anfang des Arrays alle vier Elemente der ersten Zeile (oder Spalte) und die ersten beiden Elemente der zweiten Zeile (oder Spalte) kopiert.For example, if an array has three rows (or columns) with four elements each, copying six elements from the beginning of the array would copy all four elements of the first row (or column) and the first two elements of the second row (or column). Um mit dem Kopieren vom zweiten Element der dritten Zeile (oder Spalte) zu beginnen, muss sourceIndex
die obere Grenze der ersten Zeile (oder Spalte) plus der Länge der zweiten Zeile (oder Spalte) plus 2 sein.To start copying from the second element of the third row (or column), sourceIndex
must be the upper bound of the first row (or column) plus the length of the second row (or column) plus two.
Wenn sich sourceArray
und destinationArray
überlappen, verhält sich diese Methode so, als ob die ursprünglichen Werte von sourceArray
an einem temporären Speicherort beibehalten wurden, bevor destinationArray
überschrieben wurde.If sourceArray
and destinationArray
overlap, this method behaves as if the original values of sourceArray
were preserved in a temporary location before destinationArray
is overwritten.
[C++][C++]
Diese Methode entspricht der standardmäßigen C/FunctionC++ -memmove
, nicht memcpy
.This method is equivalent to the standard C/C++ function memmove
, not memcpy
.
Arrays können aus Verweistyp Arrays oder Werttyp Arrays bestehen.The arrays can be reference-type arrays or value-type arrays. Die typdownumwandlung erfolgt nach Bedarf.Type downcasting is performed, as required.
Beim Kopieren aus einem Verweistyp Array in ein Werttyp Array wird jedes Element Unboxing und dann kopiert.When copying from a reference-type array to a value-type array, each element is unboxed and then copied. Beim Kopieren aus einem Werttyp Array in ein Verweistyp Array wird jedes Element gekapselt und dann kopiert.When copying from a value-type array to a reference-type array, each element is boxed and then copied.
Beim Kopieren aus einem Verweistyp-oder Werttyp Array in ein Object Array wird ein Object erstellt, um jeden Wert oder Verweis zu speichern und anschließend kopiert zu werden.When copying from a reference-type or value-type array to an Object array, an Object is created to hold each value or reference and then copied. Beim Kopieren aus einem Object Arrays in einen Verweistyp-oder Werttyp Array und die Zuweisung ist nicht möglich, wird eine InvalidCastException ausgelöst.When copying from an Object array to a reference-type or value-type array and the assignment is not possible, an InvalidCastException is thrown.
Wenn
sourceArray
unddestinationArray
beide als Verweistyp Arrays oder beide Arrays vom Typ Objectsind, wird eine flache Kopie ausgeführt.IfsourceArray
anddestinationArray
are both reference-type arrays or are both arrays of type Object, a shallow copy is performed. Eine flache Kopie eines Array ist ein neues Array, das Verweise auf dieselben Elemente wie die ursprüngliche Arrayenthält.A shallow copy of an Array is a new Array containing references to the same elements as the original Array. Die Elemente selbst oder alle Elemente, auf die von den Elementen verwiesen wird, werden nicht kopiert.The elements themselves or anything referenced by the elements are not copied. Im Gegensatz dazu kopiert eine tiefe Kopie einer Array die Elemente und alles, auf das von den Elementen direkt oder indirekt verwiesen wird.In contrast, a deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements.
Eine ArrayTypeMismatchException wird ausgelöst, wenn die Arrays nicht kompatible Typen sind.An ArrayTypeMismatchException is thrown if the arrays are of incompatible types. Typkompatibilität wird wie folgt definiert:Type compatibility is defined as follows:
Ein Typ ist mit sich selbst kompatibel.A type is compatible with itself.
Ein Werttyp ist kompatibel mit Object und mit einem Schnittstellentyp, der durch diesen Werttyp implementiert wird.A value type is compatible with Object and with an interface type implemented by that value type. Ein Werttyp wird nur dann als verbunden mit einer Schnittstelle betrachtet, wenn er diese Schnittstelle direkt implementiert.A value type is considered connected to an interface only if it implements that interface directly. Getrennte Typen sind nicht kompatibel.Disconnected types are not compatible.
Zwei intrinsische (vordefinierte) Werttypen sind kompatibel, wenn das Kopieren vom Quelltyp in den Zieltyp eine erweiternde Konvertierung ist.Two intrinsic (predefined) value types are compatible if copying from the source type to the destination type is a widening conversion. Eine erweiternde Konvertierung verliert niemals Informationen, während eine einschränkende Konvertierung Informationen verlieren kann.A widening conversion never loses information, whereas a narrowing conversion can lose information. Beispielsweise ist das Konvertieren einer 32-Bit-Ganzzahl mit Vorzeichen in eine 64-Bit-Ganzzahl mit Vorzeichen eine erweiternde Konvertierung, und die Konvertierung einer 64-Bit-Ganzzahl mit Vorzeichen in 32 eine Ganzzahl mit Vorzeichen mit Vorzeichen ist eine einschränkende KonvertierungFor example, converting a 32-bit signed integer to a 64-bit signed integer is a widening conversion, and converting a 64-bit signed integer to a 32-bit signed integer is a narrowing conversion. Weitere Informationen zu Konvertierungen finden Sie unter Convert.For more information about conversions, see Convert.
Ein nicht System interner (benutzerdefinierter) Werttyp ist nur mit sich selbst kompatibel.A nonintrinsic (user-defined) value type is compatible only with itself.
Enumerationen verfügen über eine implizite Konvertierung in Enum und ihren zugrunde liegenden Typ.Enumerations have an implicit conversion to Enum and to their underlying type.
Wenn jedes Element in sourceArray
einen Typumwandlung erfordert (z. b. von einer Basisklasse zu einer abgeleiteten Klasse oder von einer Schnittstelle zu einem Objekt) und mindestens ein Element in destinationArray
nicht in den entsprechenden Typ umgewandelt werden kann, wird ein InvalidCastException ausgelöst.If every element in sourceArray
requires a downcast (for example, from a base class to a derived class or from an interface to an object) and one or more elements cannot be cast to the corresponding type in destinationArray
, an InvalidCastException is thrown.
Wenn diese Methode eine Ausnahme beim Kopieren auslöst, ist der Status destinationArray
nicht definiert.If this method throws an exception while copying, the state of destinationArray
is undefined.
Bei dieser Methode handelt es sich um einen O (n
)-Vorgang, bei dem n
length
ist.This method is an O(n
) operation, where n
is length
.
Siehe auch
Copy(Array, Int32, Array, Int32, Int32)
Kopiert einen beim angegebenen Quellindex beginnenden Elementbereich aus einem Array und fügt ihn ab dem angegebenen Zielindex in ein anderes Array ein.Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. Die Länge und die Indizes werden als 32-Bit-Ganzzahlen angegeben.The length and the indexes are specified as 32-bit integers.
public:
static void Copy(Array ^ sourceArray, int sourceIndex, Array ^ destinationArray, int destinationIndex, int length);
public static void Copy (Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length);
static member Copy : Array * int * Array * int * int -> unit
Public Shared Sub Copy (sourceArray As Array, sourceIndex As Integer, destinationArray As Array, destinationIndex As Integer, length As Integer)
Parameter
- sourceArray
- Array
Das Array, das die zu kopierenden Daten enthält.The Array that contains the data to copy.
- sourceIndex
- Int32
Eine 32-Bit-Ganzzahl, die den Index im sourceArray
angibt, ab dem kopiert werden soll.A 32-bit integer that represents the index in the sourceArray
at which copying begins.
- destinationIndex
- Int32
Eine 32-Bit-Ganzzahl, die den Index im destinationArray
angibt, ab dem gespeichert werden soll.A 32-bit integer that represents the index in the destinationArray
at which storing begins.
- length
- Int32
Eine 32-Bit-Ganzzahl, die die Anzahl der zu kopierenden Elemente darstellt.A 32-bit integer that represents the number of elements to copy.
Ausnahmen
sourceArray
ist null
.sourceArray
is null
.
- oder --or-
destinationArray
ist null
.destinationArray
is null
.
sourceArray
und destinationArray
sind von unterschiedlichem Rang.sourceArray
and destinationArray
have different ranks.
sourceArray
und destinationArray
weisen inkompatible Typen auf.sourceArray
and destinationArray
are of incompatible types.
Mindestens ein Element im sourceArray
kann nicht in den Typ des destinationArray
s umgewandelt werden.At least one element in sourceArray
cannot be cast to the type of destinationArray
.
sourceIndex
ist kleiner als die untere Grenze der ersten Dimension von sourceArray
.sourceIndex
is less than the lower bound of the first dimension of sourceArray
.
- oder --or-
destinationIndex
ist kleiner als die untere Grenze der ersten Dimension von destinationArray
.destinationIndex
is less than the lower bound of the first dimension of destinationArray
.
- oder --or-
length
ist kleiner als Null.length
is less than zero.
length
ist größer als die Anzahl der Elemente vom sourceIndex
bis zum Ende des sourceArray
s.length
is greater than the number of elements from sourceIndex
to the end of sourceArray
.
- oder --or-
length
ist größer als die Anzahl der Elemente vom destinationIndex
bis zum Ende des destinationArray
s.length
is greater than the number of elements from destinationIndex
to the end of destinationArray
.
Beispiele
Im folgenden Codebeispiel wird veranschaulicht, wie von einem Array vom Typ Object in einen anderen Array vom Typ Integer kopiert wird.The following code example shows how to copy from one Array of type Object to another Array of type integer.
using namespace System;
void PrintValues( Array^ myArr );
void main()
{
// Creates and initializes a new Array instance of type Int32.
Array^ myIntArray = Array::CreateInstance( Type::GetType( "System.Int32" ), 5 );
for ( int i = myIntArray->GetLowerBound( 0 ); i <= myIntArray->GetUpperBound( 0 ); i++ )
myIntArray->SetValue( i + 1, i );
// Creates and initializes a new Array instance of type Object.
Array^ myObjArray = Array::CreateInstance( Type::GetType( "System.Object" ), 5 );
for ( int i = myObjArray->GetLowerBound( 0 ); i <= myObjArray->GetUpperBound( 0 ); i++ )
myObjArray->SetValue( i + 26, i );
// Displays the initial values of both arrays.
Console::WriteLine( "Int32 array:" );
PrintValues( myIntArray );
Console::WriteLine( "Object array:" );
PrintValues( myObjArray );
// Copies the first element from the Int32 array to the Object array.
Array::Copy( myIntArray, myIntArray->GetLowerBound( 0 ), myObjArray, myObjArray->GetLowerBound( 0 ), 1 );
// Copies the last two elements from the Object array to the Int32 array.
Array::Copy( myObjArray, myObjArray->GetUpperBound( 0 ) - 1, myIntArray, myIntArray->GetUpperBound( 0 ) - 1, 2 );
// Displays the values of the modified arrays.
Console::WriteLine( "Int32 array - Last two elements should now be the same as Object array:" );
PrintValues( myIntArray );
Console::WriteLine( "Object array - First element should now be the same as Int32 array:" );
PrintValues( myObjArray );
}
void PrintValues( Array^ myArr )
{
System::Collections::IEnumerator^ myEnumerator = myArr->GetEnumerator();
int i = 0;
int cols = myArr->GetLength( myArr->Rank - 1 );
while ( myEnumerator->MoveNext() )
{
if ( i < cols )
{
i++;
}
else
{
Console::WriteLine();
i = 1;
}
Console::Write( "\t{0}", myEnumerator->Current );
}
Console::WriteLine();
}
/*
This code produces the following output.
Int32 array:
1 2 3 4 5
Object array:
26 27 28 29 30
Int32 array - Last two elements should now be the same as Object array:
1 2 3 29 30
Object array - First element should now be the same as Int32 array:
1 27 28 29 30
*/
using System;
public class SamplesArray {
public static void Main() {
// Creates and initializes a new Array of type Int32.
Array myIntArray=Array.CreateInstance( typeof(System.Int32), 5 );
for ( int i = myIntArray.GetLowerBound(0); i <= myIntArray.GetUpperBound(0); i++ )
myIntArray.SetValue( i+1, i );
// Creates and initializes a new Array of type Object.
Array myObjArray = Array.CreateInstance( typeof(System.Object), 5 );
for ( int i = myObjArray.GetLowerBound(0); i <= myObjArray.GetUpperBound(0); i++ )
myObjArray.SetValue( i+26, i );
// Displays the initial values of both arrays.
Console.WriteLine( "Int32 array:" );
PrintValues( myIntArray );
Console.WriteLine( "Object array:" );
PrintValues( myObjArray );
// Copies the first element from the Int32 array to the Object array.
Array.Copy( myIntArray, myIntArray.GetLowerBound(0), myObjArray, myObjArray.GetLowerBound(0), 1 );
// Copies the last two elements from the Object array to the Int32 array.
Array.Copy( myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2 );
// Displays the values of the modified arrays.
Console.WriteLine( "Int32 array - Last two elements should now be the same as Object array:" );
PrintValues( myIntArray );
Console.WriteLine( "Object array - First element should now be the same as Int32 array:" );
PrintValues( myObjArray );
}
public static void PrintValues( Array myArr ) {
System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
int i = 0;
int cols = myArr.GetLength( myArr.Rank - 1 );
while ( myEnumerator.MoveNext() ) {
if ( i < cols ) {
i++;
} else {
Console.WriteLine();
i = 1;
}
Console.Write( "\t{0}", myEnumerator.Current );
}
Console.WriteLine();
}
}
/*
This code produces the following output.
Int32 array:
1 2 3 4 5
Object array:
26 27 28 29 30
Int32 array - Last two elements should now be the same as Object array:
1 2 3 29 30
Object array - First element should now be the same as Int32 array:
1 27 28 29 30
*/
Public Class SamplesArray
Public Shared Sub Main()
' Creates and initializes a new Array of type Int32.
Dim myIntArray As Array = _
Array.CreateInstance(GetType(System.Int32), 5)
Dim i As Integer
For i = myIntArray.GetLowerBound(0) To myIntArray.GetUpperBound(0)
myIntArray.SetValue(i + 1, i)
Next i
' Creates and initializes a new Array of type Object.
Dim myObjArray As Array = _
Array.CreateInstance(GetType(System.Object), 5)
For i = myObjArray.GetLowerBound(0) To myObjArray.GetUpperBound(0)
myObjArray.SetValue(i + 26, i)
Next i
' Displays the initial values of both arrays.
Console.WriteLine("Int32 array:")
PrintValues(myIntArray)
Console.WriteLine("Object array:")
PrintValues(myObjArray)
' Copies the first element from the Int32 array to the Object array.
Array.Copy(myIntArray, myIntArray.GetLowerBound(0), myObjArray, _
myObjArray.GetLowerBound(0), 1)
' Copies the last two elements from the Object array to the Int32 array.
Array.Copy(myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, _
myIntArray.GetUpperBound(0) - 1, 2)
' Displays the values of the modified arrays.
Console.WriteLine("Int32 array - Last two elements should now be " _
+ "the same as Object array:")
PrintValues(myIntArray)
Console.WriteLine("Object array - First element should now be the " _
+ "same as Int32 array:")
PrintValues(myObjArray)
End Sub
Public Shared Sub PrintValues(myArr As Array)
Dim myEnumerator As System.Collections.IEnumerator = _
myArr.GetEnumerator()
Dim i As Integer = 0
Dim cols As Integer = myArr.GetLength((myArr.Rank - 1))
While myEnumerator.MoveNext()
If i < cols Then
i += 1
Else
Console.WriteLine()
i = 1
End If
Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
End While
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
' Int32 array:
' 1 2 3 4 5
' Object array:
' 26 27 28 29 30
' Int32 array - Last two elements should now be the same as Object array:
' 1 2 3 29 30
' Object array - First element should now be the same as Int32 array:
' 1 27 28 29 30
Hinweise
Die Parameter "sourceArray
" und "destinationArray
" müssen die gleiche Anzahl von Dimensionen aufweisen.The sourceArray
and destinationArray
parameters must have the same number of dimensions. Außerdem muss destinationArray
bereits dimensioniert sein und muss über eine ausreichende Anzahl von Elementen verfügen, beginnend mit der destinationIndex
Position, um die kopierten Daten aufzunehmen.In addition, destinationArray
must already have been dimensioned and must have a sufficient number of elements starting from the destinationIndex
position to accommodate the copied data.
Beim Kopieren zwischen mehrdimensionalen Arrays verhält sich das Array wie ein langes eindimensionales Array, bei dem die Zeilen (oder Spalten) vom Ende bis zum Ende verankert werden.When copying between multidimensional arrays, the array behaves like a long one-dimensional array, where the rows (or columns) are conceptually laid end-to-end. Wenn ein Array z. b. über drei Zeilen (oder Spalten) mit jeweils vier Elementen verfügt, werden beim Kopieren von sechs Elementen vom Anfang des Arrays alle vier Elemente der ersten Zeile (oder Spalte) und die ersten beiden Elemente der zweiten Zeile (oder Spalte) kopiert.For example, if an array has three rows (or columns) with four elements each, copying six elements from the beginning of the array would copy all four elements of the first row (or column) and the first two elements of the second row (or column). Um mit dem Kopieren vom zweiten Element der dritten Zeile (oder Spalte) zu beginnen, muss sourceIndex
die obere Grenze der ersten Zeile (oder Spalte) plus der Länge der zweiten Zeile (oder Spalte) plus 2 sein.To start copying from the second element of the third row (or column), sourceIndex
must be the upper bound of the first row (or column) plus the length of the second row (or column) plus two.
Wenn sich sourceArray
und destinationArray
überlappen, verhält sich diese Methode so, als ob die ursprünglichen Werte von sourceArray
an einem temporären Speicherort beibehalten wurden, bevor destinationArray
überschrieben wurde.If sourceArray
and destinationArray
overlap, this method behaves as if the original values of sourceArray
were preserved in a temporary location before destinationArray
is overwritten.
[C++][C++]
Diese Methode entspricht der standardmäßigen C/FunctionC++ -memmove
, nicht memcpy
.This method is equivalent to the standard C/C++ function memmove
, not memcpy
.
Arrays können aus Verweistyp Arrays oder Werttyp Arrays bestehen.The arrays can be reference-type arrays or value-type arrays. Die typdownumwandlung erfolgt nach Bedarf.Type downcasting is performed, as required.
Beim Kopieren aus einem Verweistyp Array in ein Werttyp Array wird jedes Element Unboxing und dann kopiert.When copying from a reference-type array to a value-type array, each element is unboxed and then copied. Beim Kopieren aus einem Werttyp Array in ein Verweistyp Array wird jedes Element gekapselt und dann kopiert.When copying from a value-type array to a reference-type array, each element is boxed and then copied.
Beim Kopieren aus einem Verweistyp-oder Werttyp Array in ein Object Array wird ein Object erstellt, um jeden Wert oder Verweis zu speichern und anschließend kopiert zu werden.When copying from a reference-type or value-type array to an Object array, an Object is created to hold each value or reference and then copied. Beim Kopieren aus einem Object Arrays in einen Verweistyp-oder Werttyp Array und die Zuweisung ist nicht möglich, wird eine InvalidCastException ausgelöst.When copying from an Object array to a reference-type or value-type array and the assignment is not possible, an InvalidCastException is thrown.
Wenn
sourceArray
unddestinationArray
beide als Verweistyp Arrays oder beide Arrays vom Typ Objectsind, wird eine flache Kopie ausgeführt.IfsourceArray
anddestinationArray
are both reference-type arrays or are both arrays of type Object, a shallow copy is performed. Eine flache Kopie eines Array ist ein neues Array, das Verweise auf dieselben Elemente wie die ursprüngliche Arrayenthält.A shallow copy of an Array is a new Array containing references to the same elements as the original Array. Die Elemente selbst oder alle Elemente, auf die von den Elementen verwiesen wird, werden nicht kopiert.The elements themselves or anything referenced by the elements are not copied. Im Gegensatz dazu kopiert eine tiefe Kopie einer Array die Elemente und alles, auf das von den Elementen direkt oder indirekt verwiesen wird.In contrast, a deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements.
Eine ArrayTypeMismatchException wird ausgelöst, wenn die Arrays nicht kompatible Typen sind.An ArrayTypeMismatchException is thrown if the arrays are of incompatible types. Typkompatibilität wird wie folgt definiert:Type compatibility is defined as follows:
Ein Typ ist mit sich selbst kompatibel.A type is compatible with itself.
Ein Werttyp ist kompatibel mit Object und mit einem Schnittstellentyp, der durch diesen Werttyp implementiert wird.A value type is compatible with Object and with an interface type implemented by that value type. Ein Werttyp wird nur dann als verbunden mit einer Schnittstelle betrachtet, wenn er diese Schnittstelle direkt implementiert.A value type is considered connected to an interface only if it implements that interface directly. Getrennte Typen sind nicht kompatibel.Disconnected types are not compatible.
Zwei intrinsische (vordefinierte) Werttypen sind kompatibel, wenn das Kopieren vom Quelltyp in den Zieltyp eine erweiternde Konvertierung ist.Two intrinsic (predefined) value types are compatible if copying from the source type to the destination type is a widening conversion. Eine erweiternde Konvertierung verliert niemals Informationen, während eine einschränkende Konvertierung Informationen verlieren kann.A widening conversion never loses information, whereas a narrowing conversion can lose information. Beispielsweise ist das Konvertieren einer 32-Bit-Ganzzahl mit Vorzeichen in eine 64-Bit-Ganzzahl mit Vorzeichen eine erweiternde Konvertierung, und die Konvertierung einer 64-Bit-Ganzzahl mit Vorzeichen in 32 eine Ganzzahl mit Vorzeichen mit Vorzeichen ist eine einschränkende KonvertierungFor example, converting a 32-bit signed integer to a 64-bit signed integer is a widening conversion, and converting a 64-bit signed integer to a 32-bit signed integer is a narrowing conversion. Weitere Informationen zu Konvertierungen finden Sie unter Convert.For more information about conversions, see Convert.
Ein nicht System interner (benutzerdefinierter) Werttyp ist nur mit sich selbst kompatibel.A nonintrinsic (user-defined) value type is compatible only with itself.
Enumerationen verfügen über eine implizite Konvertierung in Enum und ihren zugrunde liegenden Typ.Enumerations have an implicit conversion to Enum and to their underlying type.
Wenn jedes Element in sourceArray
einen Typumwandlung erfordert (z. b. von einer Basisklasse zu einer abgeleiteten Klasse oder von einer Schnittstelle zu einem Objekt) und mindestens ein Element in destinationArray
nicht in den entsprechenden Typ umgewandelt werden kann, wird ein InvalidCastException ausgelöst.If every element in sourceArray
requires a downcast (for example, from a base class to a derived class or from an interface to an object) and one or more elements cannot be cast to the corresponding type in destinationArray
, an InvalidCastException is thrown.
Wenn diese Methode eine Ausnahme beim Kopieren auslöst, ist der Status destinationArray
nicht definiert.If this method throws an exception while copying, the state of destinationArray
is undefined.
Bei dieser Methode handelt es sich um einen O (n
)-Vorgang, bei dem n
length
ist.This method is an O(n
) operation, where n
is length
.
Siehe auch
Copy(Array, Array, Int64)
Kopiert einen mit dem ersten Element beginnenden Elementbereich eines Array und fügt ihn ab dem ersten Element in ein anderes Array ein.Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. Die Länge wird als 64-Bit-Ganzzahl angegeben.The length is specified as a 64-bit integer.
public:
static void Copy(Array ^ sourceArray, Array ^ destinationArray, long length);
public static void Copy (Array sourceArray, Array destinationArray, long length);
static member Copy : Array * Array * int64 -> unit
Public Shared Sub Copy (sourceArray As Array, destinationArray As Array, length As Long)
Parameter
- sourceArray
- Array
Das Array, das die zu kopierenden Daten enthält.The Array that contains the data to copy.
- length
- Int64
Eine 64-Bit-Ganzzahl, die die Anzahl der zu kopierenden Elemente darstellt.A 64-bit integer that represents the number of elements to copy. Die Ganzzahl muss zwischen 0 (null) und einschließlich MaxValue liegen.The integer must be between zero and MaxValue, inclusive.
Ausnahmen
sourceArray
ist null
.sourceArray
is null
.
- oder --or-
destinationArray
ist null
.destinationArray
is null
.
sourceArray
und destinationArray
sind von unterschiedlichem Rang.sourceArray
and destinationArray
have different ranks.
sourceArray
und destinationArray
weisen inkompatible Typen auf.sourceArray
and destinationArray
are of incompatible types.
Mindestens ein Element im sourceArray
kann nicht in den Typ des destinationArray
s umgewandelt werden.At least one element in sourceArray
cannot be cast to the type of destinationArray
.
length
ist größer als die Anzahl von Elementen in sourceArray
.length
is greater than the number of elements in sourceArray
.
- oder --or-
length
ist größer als die Anzahl von Elementen in destinationArray
.length
is greater than the number of elements in destinationArray
.
Hinweise
Die Parameter "sourceArray
" und "destinationArray
" müssen die gleiche Anzahl von Dimensionen aufweisen.The sourceArray
and destinationArray
parameters must have the same number of dimensions. Außerdem muss destinationArray
bereits dimensioniert sein und muss über eine ausreichende Anzahl von Elementen verfügen, um die kopierten Daten aufnehmen zu können.In addition, destinationArray
must already have been dimensioned and must have a sufficient number of elements to accommodate the copied data.
Beim Kopieren zwischen mehrdimensionalen Arrays verhält sich das Array wie ein langes eindimensionales Array, bei dem die Zeilen (oder Spalten) konzeptionell am Ende angeordnet werden.When copying between multidimensional arrays, the array behaves like a long one-dimensional array, where the rows (or columns) are conceptually laid end to end. Wenn ein Array z. b. über drei Zeilen (oder Spalten) mit jeweils vier Elementen verfügt, werden beim Kopieren von sechs Elementen vom Anfang des Arrays alle vier Elemente der ersten Zeile (oder Spalte) und die ersten beiden Elemente der zweiten Zeile (oder Spalte) kopiert.For example, if an array has three rows (or columns) with four elements each, copying six elements from the beginning of the array would copy all four elements of the first row (or column) and the first two elements of the second row (or column).
Wenn sich sourceArray
und destinationArray
überlappen, verhält sich diese Methode so, als ob die ursprünglichen Werte von sourceArray
an einem temporären Speicherort beibehalten wurden, bevor destinationArray
überschrieben wurde.If sourceArray
and destinationArray
overlap, this method behaves as if the original values of sourceArray
were preserved in a temporary location before destinationArray
is overwritten.
[C++][C++]
Diese Methode entspricht der standardmäßigen C/FunctionC++ -memmove
, nicht memcpy
.This method is equivalent to the standard C/C++ function memmove
, not memcpy
.
Arrays können aus Verweistyp Arrays oder Werttyp Arrays bestehen.The arrays can be reference-type arrays or value-type arrays. Die typdownumwandlung erfolgt nach Bedarf.Type downcasting is performed, as required.
Beim Kopieren aus einem Verweistyp Array in ein Werttyp Array wird jedes Element Unboxing und dann kopiert.When copying from a reference-type array to a value-type array, each element is unboxed and then copied. Beim Kopieren aus einem Werttyp Array in ein Verweistyp Array wird jedes Element gekapselt und dann kopiert.When copying from a value-type array to a reference-type array, each element is boxed and then copied.
Beim Kopieren aus einem Verweistyp-oder Werttyp Array in ein Object Array wird ein Object erstellt, um jeden Wert oder Verweis zu speichern und anschließend kopiert zu werden.When copying from a reference-type or value-type array to an Object array, an Object is created to hold each value or reference and then copied. Beim Kopieren aus einem Object Arrays in einen Verweistyp-oder Werttyp Array und die Zuweisung ist nicht möglich, wird eine InvalidCastException ausgelöst.When copying from an Object array to a reference-type or value-type array and the assignment is not possible, an InvalidCastException is thrown.
Wenn
sourceArray
unddestinationArray
beide als Verweistyp Arrays oder beide Arrays vom Typ Objectsind, wird eine flache Kopie ausgeführt.IfsourceArray
anddestinationArray
are both reference-type arrays or are both arrays of type Object, a shallow copy is performed. Eine flache Kopie eines Array ist ein neues Array, das Verweise auf dieselben Elemente wie die ursprüngliche Arrayenthält.A shallow copy of an Array is a new Array containing references to the same elements as the original Array. Die Elemente selbst oder alle Elemente, auf die von den Elementen verwiesen wird, werden nicht kopiert.The elements themselves or anything referenced by the elements are not copied. Im Gegensatz dazu kopiert eine tiefe Kopie einer Array die Elemente und alles, auf das von den Elementen direkt oder indirekt verwiesen wird.In contrast, a deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements.
Eine ArrayTypeMismatchException wird ausgelöst, wenn die Arrays nicht kompatible Typen sind.An ArrayTypeMismatchException is thrown if the arrays are of incompatible types. Typkompatibilität wird wie folgt definiert:Type compatibility is defined as follows:
Ein Typ ist mit sich selbst kompatibel.A type is compatible with itself.
Ein Werttyp ist kompatibel mit Object und mit einem Schnittstellentyp, der durch diesen Werttyp implementiert wird.A value type is compatible with Object and with an interface type implemented by that value type. Ein Werttyp wird nur dann als verbunden mit einer Schnittstelle betrachtet, wenn er diese Schnittstelle direkt implementiert.A value type is considered connected to an interface only if it implements that interface directly. Getrennte Typen sind nicht kompatibel.Disconnected types are not compatible.
Zwei intrinsische (vordefinierte) Werttypen sind kompatibel, wenn das Kopieren vom Quelltyp in den Zieltyp eine erweiternde Konvertierung ist.Two intrinsic (predefined) value types are compatible if copying from the source type to the destination type is a widening conversion. Eine erweiternde Konvertierung verliert niemals Informationen, während eine einschränkende Konvertierung Informationen verlieren kann.A widening conversion never loses information, whereas a narrowing conversion can lose information. Beispielsweise ist das Konvertieren einer 32-Bit-Ganzzahl mit Vorzeichen in eine 64-Bit-Ganzzahl mit Vorzeichen eine erweiternde Konvertierung, und die Konvertierung einer 64-Bit-Ganzzahl mit Vorzeichen in 32 eine Ganzzahl mit Vorzeichen mit Vorzeichen ist eine einschränkende KonvertierungFor example, converting a 32-bit signed integer to a 64-bit signed integer is a widening conversion, and converting a 64-bit signed integer to a 32-bit signed integer is a narrowing conversion. Weitere Informationen zu Konvertierungen finden Sie unter Convert.For more information about conversions, see Convert.
Ein nicht System interner (benutzerdefinierter) Werttyp ist nur mit sich selbst kompatibel.A nonintrinsic (user-defined) value type is compatible only with itself.
Enumerationen verfügen über eine implizite Konvertierung in Enum und ihren zugrunde liegenden Typ.Enumerations have an implicit conversion to Enum and to their underlying type.
Wenn jedes Element in sourceArray
einen Typumwandlung erfordert (z. b. von einer Basisklasse zu einer abgeleiteten Klasse oder von einer Schnittstelle zu einem Objekt) und mindestens ein Element in destinationArray
nicht in den entsprechenden Typ umgewandelt werden kann, wird ein InvalidCastException ausgelöst.If every element in sourceArray
requires a downcast (for example, from a base class to a derived class or from an interface to an object) and one or more elements cannot be cast to the corresponding type in destinationArray
, an InvalidCastException is thrown.
Wenn diese Methode eine Ausnahme beim Kopieren auslöst, ist der Status destinationArray
nicht definiert.If this method throws an exception while copying, the state of destinationArray
is undefined.
Bei dieser Methode handelt es sich um einen O (n
)-Vorgang, bei dem n
length
ist.This method is an O(n
) operation, where n
is length
.
Siehe auch
Copy(Array, Array, Int32)
Kopiert einen mit dem ersten Element beginnenden Elementbereich eines Array und fügt ihn ab dem ersten Element in ein anderes Array ein.Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. Die Länge wird als 32-Bit-Ganzzahl angegeben.The length is specified as a 32-bit integer.
public:
static void Copy(Array ^ sourceArray, Array ^ destinationArray, int length);
public static void Copy (Array sourceArray, Array destinationArray, int length);
static member Copy : Array * Array * int -> unit
Public Shared Sub Copy (sourceArray As Array, destinationArray As Array, length As Integer)
Parameter
- sourceArray
- Array
Das Array, das die zu kopierenden Daten enthält.The Array that contains the data to copy.
- length
- Int32
Eine 32-Bit-Ganzzahl, die die Anzahl der zu kopierenden Elemente darstellt.A 32-bit integer that represents the number of elements to copy.
Ausnahmen
sourceArray
ist null
.sourceArray
is null
.
- oder --or-
destinationArray
ist null
.destinationArray
is null
.
sourceArray
und destinationArray
sind von unterschiedlichem Rang.sourceArray
and destinationArray
have different ranks.
sourceArray
und destinationArray
weisen inkompatible Typen auf.sourceArray
and destinationArray
are of incompatible types.
Mindestens ein Element in sourceArray
kann nicht in den destinationArray
-Typ umgewandelt werden.At least one element in sourceArray
cannot be cast to the type of destinationArray
.
length
ist kleiner als Null.length
is less than zero.
length
ist größer als die Anzahl von Elementen in sourceArray
.length
is greater than the number of elements in sourceArray
.
- oder --or-
length
ist größer als die Anzahl von Elementen in destinationArray
.length
is greater than the number of elements in destinationArray
.
Hinweise
Die Parameter "sourceArray
" und "destinationArray
" müssen die gleiche Anzahl von Dimensionen aufweisen.The sourceArray
and destinationArray
parameters must have the same number of dimensions. Außerdem muss destinationArray
bereits dimensioniert sein und muss über eine ausreichende Anzahl von Elementen verfügen, um die kopierten Daten aufnehmen zu können.In addition, destinationArray
must already have been dimensioned and must have a sufficient number of elements to accommodate the copied data.
Beim Kopieren zwischen mehrdimensionalen Arrays verhält sich das Array wie ein langes eindimensionales Array, bei dem die Zeilen (oder Spalten) konzeptionell am Ende angeordnet werden.When copying between multidimensional arrays, the array behaves like a long one-dimensional array, where the rows (or columns) are conceptually laid end to end. Wenn ein Array z. b. über drei Zeilen (oder Spalten) mit jeweils vier Elementen verfügt, werden beim Kopieren von sechs Elementen vom Anfang des Arrays alle vier Elemente der ersten Zeile (oder Spalte) und die ersten beiden Elemente der zweiten Zeile (oder Spalte) kopiert.For example, if an array has three rows (or columns) with four elements each, copying six elements from the beginning of the array would copy all four elements of the first row (or column) and the first two elements of the second row (or column).
Wenn sich sourceArray
und destinationArray
überlappen, verhält sich diese Methode so, als ob die ursprünglichen Werte von sourceArray
an einem temporären Speicherort beibehalten wurden, bevor destinationArray
überschrieben wurde.If sourceArray
and destinationArray
overlap, this method behaves as if the original values of sourceArray
were preserved in a temporary location before destinationArray
is overwritten.
[C++][C++]
Diese Methode entspricht der standardmäßigen C/FunctionC++ -memmove
, nicht memcpy
.This method is equivalent to the standard C/C++ function memmove
, not memcpy
.
Arrays können aus Verweistyp Arrays oder Werttyp Arrays bestehen.The arrays can be reference-type arrays or value-type arrays. Die typdownumwandlung erfolgt nach Bedarf.Type downcasting is performed, as required.
Beim Kopieren aus einem Verweistyp Array in ein Werttyp Array wird jedes Element Unboxing und dann kopiert.When copying from a reference-type array to a value-type array, each element is unboxed and then copied. Beim Kopieren aus einem Werttyp Array in ein Verweistyp Array wird jedes Element gekapselt und dann kopiert.When copying from a value-type array to a reference-type array, each element is boxed and then copied.
Beim Kopieren aus einem Verweistyp-oder Werttyp Array in ein Object Array wird ein Object erstellt, um jeden Wert oder Verweis zu speichern und anschließend kopiert zu werden.When copying from a reference-type or value-type array to an Object array, an Object is created to hold each value or reference and then copied. Beim Kopieren aus einem Object Arrays in einen Verweistyp-oder Werttyp Array und die Zuweisung ist nicht möglich, wird eine InvalidCastException ausgelöst.When copying from an Object array to a reference-type or value-type array and the assignment is not possible, an InvalidCastException is thrown.
Wenn
sourceArray
unddestinationArray
beide als Verweistyp Arrays oder beide Arrays vom Typ Objectsind, wird eine flache Kopie ausgeführt.IfsourceArray
anddestinationArray
are both reference-type arrays or are both arrays of type Object, a shallow copy is performed. Eine flache Kopie eines Array ist ein neues Array, das Verweise auf dieselben Elemente wie die ursprüngliche Arrayenthält.A shallow copy of an Array is a new Array containing references to the same elements as the original Array. Die Elemente selbst oder alle Elemente, auf die von den Elementen verwiesen wird, werden nicht kopiert.The elements themselves or anything referenced by the elements are not copied. Im Gegensatz dazu kopiert eine tiefe Kopie einer Array die Elemente und alles, auf das von den Elementen direkt oder indirekt verwiesen wird.In contrast, a deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements.
Eine ArrayTypeMismatchException wird ausgelöst, wenn die Arrays nicht kompatible Typen sind.An ArrayTypeMismatchException is thrown if the arrays are of incompatible types. Typkompatibilität wird wie folgt definiert:Type compatibility is defined as follows:
Ein Typ ist mit sich selbst kompatibel.A type is compatible with itself.
Ein Werttyp ist kompatibel mit Object und mit einem Schnittstellentyp, der durch diesen Werttyp implementiert wird.A value type is compatible with Object and with an interface type implemented by that value type. Ein Werttyp wird nur dann als verbunden mit einer Schnittstelle betrachtet, wenn er diese Schnittstelle direkt implementiert.A value type is considered connected to an interface only if it implements that interface directly. Getrennte Typen sind nicht kompatibel.Disconnected types are not compatible.
Zwei intrinsische (vordefinierte) Werttypen sind kompatibel, wenn das Kopieren vom Quelltyp in den Zieltyp eine erweiternde Konvertierung ist.Two intrinsic (predefined) value types are compatible if copying from the source type to the destination type is a widening conversion. Eine erweiternde Konvertierung verliert niemals Informationen, während eine einschränkende Konvertierung Informationen verlieren kann.A widening conversion never loses information, whereas a narrowing conversion can lose information. Beispielsweise ist das Konvertieren einer 32-Bit-Ganzzahl mit Vorzeichen in eine 64-Bit-Ganzzahl mit Vorzeichen eine erweiternde Konvertierung, und die Konvertierung einer 64-Bit-Ganzzahl mit Vorzeichen in 32 eine Ganzzahl mit Vorzeichen mit Vorzeichen ist eine einschränkende KonvertierungFor example, converting a 32-bit signed integer to a 64-bit signed integer is a widening conversion, and converting a 64-bit signed integer to a 32-bit signed integer is a narrowing conversion. Weitere Informationen zu Konvertierungen finden Sie unter Convert.For more information about conversions, see Convert.
Ein nicht System interner (benutzerdefinierter) Werttyp ist nur mit sich selbst kompatibel.A nonintrinsic (user-defined) value type is compatible only with itself.
Enumerationen verfügen über eine implizite Konvertierung in Enum und ihren zugrunde liegenden Typ.Enumerations have an implicit conversion to Enum and to their underlying type.
Wenn jedes Element in sourceArray
einen Typumwandlung erfordert (z. b. von einer Basisklasse zu einer abgeleiteten Klasse oder von einer Schnittstelle zu einem Objekt) und mindestens ein Element in destinationArray
nicht in den entsprechenden Typ umgewandelt werden kann, wird ein InvalidCastException ausgelöst.If every element in sourceArray
requires a downcast (for example, from a base class to a derived class or from an interface to an object) and one or more elements cannot be cast to the corresponding type in destinationArray
, an InvalidCastException is thrown.
Wenn diese Methode eine Ausnahme beim Kopieren auslöst, ist der Status destinationArray
nicht definiert.If this method throws an exception while copying, the state of destinationArray
is undefined.
Bei dieser Methode handelt es sich um einen O (n
)-Vorgang, bei dem n
length
ist.This method is an O(n
) operation, where n
is length
.