tags:

views:

62

answers:

3

if i have:

string[] myArray =  . . . .

which is an array with a length of 10. how can i create a new string array that is the 2nd to 10th elements of the first array without looping?

+4  A: 

Use System.Array.Copy:

string[] myArray = ....
string[] copy = new string[10];
Array.Copy(myArray, 2, copy, 0, 10);    
codekaizen
A: 

Array.Copy

(But note, it will be looping under the covers, just optimally so).

Example.

Mitch Wheat
lol...Just got to it 51s before you did....I like the function but, prefer loops as I can address them how I need.
Joe Garrett
A: 
   // 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, 

Look: 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 );

Joe Garrett
This is in C++, not C#.
Adam Robinson
Sorry, Quick Review....Similar in solution and nature...though.
Joe Garrett