Array modifications passed by reference

I recently came across some third-party C # encoders that do the following:

public int RecvByteDataFromPrinter(ref byte[] byteData)
{
    byte[] recvdata = new byte[1024];

    ///...fills recvdata array...       

    byteData = recvdata;
    return SUCCESS;
}

What does the " byteData = recvdata" line do in this case?

It seems like the goal is to have byteData the contents of the contents of the recvdata array. However, I got the impression that you need to perform an operation Array.Copy(...)for this to happen.

Is this really a byteData link change to point to a newly allocated array? If so, is this array guaranteed?

+3
source share
4 answers

, - ref - . ? - ? , GC'd - . () GC'd, , ...

Array.Copy , "ref", .

+7

, byteData, (- 'ref'). "" recvData (, ).

, , ( , ).

+2

recvdata byteData..NET , , recvdata, , byteData .

+1

The byteData reference will now point to the recvdata array, giving it a root. It will “stick” until all its roots disappear (that is, the caller does not get rid of the passed byteData object), and he becomes a candidate for assembly. The original array object passed in is a candidate for collection as soon as the method returns.

0
source

All Articles