Copy part of an array to another array

Possible duplicate:
How to copy part of an array to another array in C #?

if I have:

string[] myArray =  . . . .

which is an array with a length of 10. How can I create a new string array, which is the 2-10th elements of the first array without a loop?

+3
source share
3 answers

Use System.Array.Copy :

string[] myArray = ....
string[] copy = new string[10];
Array.Copy(myArray, 2, copy, 0, 10);    
+9
source

Array.Copy

(But mind you, it will loop under the covers, just optimally).

An example .

0
source
   // 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, 

: http://msdn.microsoft.com/en-us/library/system.array.copy%28VS.71%29.aspx

Array.Copy(myIntArray, myIntArray.GetLowerBound(0), myObjArray, myObjArray.GetLowerBound(0), 1);

-1

Source: https://habr.com/ru/post/1754175/


All Articles