0

Is it possible to have an array which points to other arrays in C#?

double[] arr1 = new double[2]{1,2};
double[] arr2 = new double[2]{3,4};
double[] mergedArr = arr1 + arr2; //of course not working like that, but how to do it right?

So when I change a value in arr1 the value in the mergedArray automatically changes?

3
  • new arr1[2]{1,2} is not valid syntax. You need the type in the initialization, and also if you have values defined the length of the array can't be specified. It is either new double[2]; or new double[]{1,2}; Commented Sep 7, 2022 at 13:08
  • of course, edited, ty Commented Sep 7, 2022 at 13:36
  • Still wrong. Try again. It needs to be new double[]{1,2}; and so on. Commented Sep 7, 2022 at 14:12

4 Answers 4

2

You should use IEnumerable, then you can use LINQ's Concat

IEnumerable<double> mergedArr = arr1.Concat(arr2);

This does not create a new object unless you call ToArray or ToList on it

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

Comments

1

You could do something like this using Span<T> or Memory<T>

var arr = new double[]{1, 2, 3, 4};
var span1= arr.AsSpan(0, 2);
var span2= arr.AsSpan(2, 2);
span2[0] = 5;
// arr is now {1, 2, 5, 4}

Span<T> work kind of like a smart pointer that lets you refer to memory inside an array. But you have to start with the actual array, and create your spans from this, you cannot "merge" arrays, since they are different objects and will be placed in non-continous memory.

Note that Spans is very lightweight and cheap, but has some restrictions on how it can be used, Memory<T> is slightly less lightweight but removes some of the restrictions, I recommend reading the documentation to avoid any surprises.

6 Comments

oh this sounds pretty good actually! Defining the big array first is not a problem in my case. So to have this double proof : In that case the array size is not twice produced in the memory?
@c_ph_r no, Spans is a lightweight type that is just a "managed pointer" and a length, there will be no copying of any data. Spans where introduced as a safe alternative to pointers, in part avoid having to copy strings when parsing webrequests, and instead use Span<char>.
Fantastic! Did not know that this is implemented in C#...very nice! I guess they aren't threadsafe? So I can't simultaneously fill span1 and span2?
@c_ph_r they are no more or less thread safe than a regular array or pointer. In both cases you can safely write data concurrently as long as there is no overlap in the written data. At least when doing aligned writes of words, I'm not 100% sure about the memory guarantees when doing unaligned writes and similar esoteric details.
This does the opposite of the original question. In the question the op is asking how to create an array from two sub-arrays, and in this answer, you create two sub-arrays from a big array. It might work for op, but this means the question is not clear on what they want.
|
1

Points to other arrays -> this is not possible I think. Not sure. You can copy arrays to new array.

double[] arr1 = new double[2]{1,2};
double[] arr2 = new double[2]{3,4};
double[] arr3 = new double[arr1.Length +arr2.Length];
arr1.CopyTo(arr3, 0);
arr2.CopyTo(arr3 , arr1.Length);

1 Comment

But in that case you create a new object and lose the reference right?
0

I think you are asking for a jagged array

double[] arr1 = new double[] {1, 2};
double[] arr2 = new double[] {3, 4};
double[][] jaggedArr = new double[][] { arr1, arr2};

which can be accessed via jaggedArr[0][1] => first array (0 index) and second element (1 index).

This creates an array of arrays { {1 ,2}, {3, 4} }

You can manually fill in the array of arrays which gives you more flexibility. For example

double[][] jaggedArr = new double[2][];
jaggedArr[0] = arr1;
jaggedArr[1] = arr2;

Unless you want to create an array from the values of the two arrays concatenated together (one after the other). Then you do

double[] concatArr = arr1.Concat(arr2).ToArray();

This creates a single array with values {1,2,3,4}.

6 Comments

jaggedArr is a good idea. Thing is, that in that case I have to rebuild the calls a lot, because I cannot iterate through the array as if it was just one array. Variant #2 (concat) creates a new object in the memory, doesnt it?
Indeed concat creates a copy.
Concat does not create a copy, ToArray does
@Charlieface - Well yes and no. If you didn;t have ToArray() and did a foreach(var item in a.Concat(b)){ } it will still create copies. It is when the LINQ operation executes that copies are made. So technically the ToArray() triggers the copy.
No it doesn't. If you modify one of the arrays in the middle of the loop you get the new value see dotnetfiddle.net/bWZdyj Not that you should do such a thing, but the point is that Concat is non-blocking (reads the inputs one-by-one) so you always get the latest value
|

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.