How to get Sub array?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I’m using a one dimensional array: byte[] bArray = new byte[100];
And I’m writing a function that return a subset of this array:
void GetSubArray(int offset, int size, ref byte[] subArray);

As you can see, the user asks for the bArray offset and size to put in the
given subArray.
How do I do that without copying the values? I want that the sub array will
referee to the same memory/values as the bArray (same value)???
 
How do I do that without copying the values? I want that the sub array will
referee to the same memory/values as the bArray (same value)???

You can't have multiple arrays refer to the same memory if that's what
you mean.



Mattias
 
You can't have multiple arrays refer to the same memory if that's what
you mean.

Would it use the same memory if the arrays contained references instead of
values.. i.e. if the array contained objects instead of bytes (byte is a
value type right?) can it be shared between arrays?
 
Yes, it is what I mean.
It’s hard to believe the Microsoft did not think of it.
I do understand that it contradict the managed concept. Nevertheless, C#
does allow us to pass a managed array by reference (ref and out) to another
function, but does not allow to export the same kind of array out of a
function (out of a class method)?
C# also allow to pass managed array (byte[] bArray = new byte[100]) kind off
array out of the component to unmanaged function that take byte* as an
argument.

So you see why I’m puzzled that there is now such decent way to do it beside
the ugly way of unsafe code or boxing.

I really hope there is such a way.
 
Would it use the same memory if the arrays contained references instead of
values.. i.e. if the array contained objects instead of bytes (byte is a
value type right?) can it be shared between arrays?

No, the arrays then contain their own copy of the object references,
even if they happen to refer to the same object.



Mattias
 
Back
Top