0

Lets say I have

int[] arraySegment1 = new int[10];
int[] arraySegment2 = new int[10];

Is there anyway to pass them into a one dimensional array by reference?

int[] array = new int[21];

//Could I now make the arraySegment1 be passed in array[0] - array[10] by reference? 
//And arraySegment2 in array[11] - array[21] passed by reference?

//Then when executing:
array[0] = 10000000;

System.Console.WriteLine(arraySegment1[0]);

//It should display 10000000
//By putting the arraySegment1 as reference in the one dimensional array: array?
1

4 Answers 4

3

Checkout the Array.Copy method which allows you to copy segments of arrays to a destination array:

int[] arraySegment1 = new int[10];
int[] arraySegment2 = new int[10];
// TODO: populate the arraySegment1 and arraySegment2 with some values

int[] array = new int[20];
Array.Copy(arraySegment1, 0, array, 0, arraySegment1.Length);
Array.Copy(arraySegment2, 0, array, arraySegment1.Length, arraySegment2.Length);

Also 20 is enough of a Length for the resulting array, not 21 if the 2 source arrays are 10 each.

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

4 Comments

I guess he wants to connect arrays.In your code,if array[0] changes will arraysegment1[0] change too ?
Of course not. Int32 is a value type. It's absolutely impossible to do that with a value type which by its very own definition is not passed by reference but always copied the value. You should use a reference type if you want to achieve that.
It has nothing to do with Int32 being a value type, and everything to do with the fact that you can't create a combined array that in reality is two or more smaller ones. It wouldn't work with reference types either. Sure, you could change the object the arrays refer to, but replacing element 0 in one of the two "sub arrays" will not propagate up to the bigger one, because you can't get that working in .NET.
I must have misunderstood what the OP is trying to achieve. I don't understand why you cannot create a combined array of 2 smaller arrays. That's exactly what my answer illustrates using the Array.Copy method. You have 2 small arrays and join them into a resulting array. As far as the value types are concerned, when you make Array.Copy with reference types, changing some element in one array will also reflect in the resulting array. That's what I meant but I might be missing something.
1

Try this one..

        int[] arraySegment1 = new int[10];
        int[] arraySegment2 = new int[10];
        int[] array = new int[21];
        arraySegment1.CopyTo(array,0);
        arraySegment2.CopyTo(array,(arraySegment1.Length));

Comments

0

No. It's not possible with standart arrays. But with generics you can do something like this:

int[] arraySegment1 = new int[10];
int[] arraySegment2 = new int[10];
// populate arrays

List<int[]> lists = new List<int[]>();
lists.Add(arraySegment1);
lists.Add(arraySegment2);

lists[0][0] = 100000;
System.Console.WriteLine(arraySegment1[0]); // then it will display 100000

Comments

-2

you typing wrong object

change the arraySegment1 in the write line to be array

System.Console.WriteLine(array[0]);

Comments

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.