Search code examples
c#pointerspass-by-referenceref

C# array access from first element only


I'm sorry if this has been asked before, but I don't even know what to search for to find an answer.

I'm fairly experienced in .net/c# etc., however, I have come across something I don't understand how works.

I'm working on a third party library where I do not have access to the source.

My question is: How is this function able to get the whole data in the array when only the first value is being passed?

Prototype:

SomeClass(ref byte pByte, int length);

Code example:

...
byte[] bArr = new byte[100];
// fill array with some data
SomeClass(ref bArr[0], bArr.Length);
...

Update: Sorry I didn't include this in the post to begin with. I am en experienced embedded fw engineer but I have also worked with c#/.net/wpf/wcf etc. for many years. So I am well aware of the difference between pass-by-value/reference and the ref modifier. My initial confusion was that I have never seen any c# function calls only passing the first element in an array (like pointers in c/c++) and the function can access the whole array. So it's more the syntax that got me. :)

Thanks to @jdweng's comment I used iLSpy to confirm Nicholas Carey's answer. The library is just a CLR wrapped c++ library where the dll importing and marshaling is done.

Thank you all for your answers. :)


Solution

  • An array is a contiguous bit of memory, calling a function like this:

    foo( ref bAarr[0], bArr.Length );
    

    It passes two things:

    • The address of the 1st element of the array, and
    • The number of elements in the array

    Your 3rd-party library is almost certainly a C/C++ DLL exposing a function signature along the lines of

    int foo( unsigned char *p, int len );
    

    An array reference like arr[n] is [roughly] the equivalent of the following pseudocode:

    • let p be the address of arr
    • let offset be n multiplied by the size in octets of the array's underlying type
    • let pElement be p + offset, the address of element n of array arr.

    pElement, when dereferenced, gives you the value of arr[n].