2

I pass data from one third-party lib to another one.

Given an object[,] with lower bounds {0,0}, I need to make object[,] with lower bounds {1,1}.

Is there a way to perform this convertion without creating new array and copying all the data?

1 Answer 1

6

Is there a way to perform this convertion without creating new array and copying all the data?

No. You can't change the size or bounds of an array once it has been created.

Sign up to request clarification or add additional context in comments.

7 Comments

what about the array.resize under the hood is creating a new array no ?
@K.B: Yes, that creates a new array and copies the data - it doesn't modify the existing array object.
@K.B. Quote from the documentation of Array.Resize: This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one. array must be a one-dimensional array.
@Heinzi thanks I have decompiled it and it's look like this
public static void Resize<T>(ref T[] array, int newSize) { if (newSize < 0) throw new ArgumentOutOfRangeException("newSize", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); T[] objArray1 = array; if (objArray1 == null) { array = new T[newSize]; } else { if (objArray1.Length == newSize) return; T[] objArray2 = new T[newSize]; Array.Copy((Array) objArray1, 0, (Array) objArray2, 0, objArray1.Length > newSize ? newSize : objArray1.Length); array = objArray2; } }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.