Given the following: byte[] sData; and a function declared as private byte[] construct_command()
if I were to then assign the result of construct_command() to sData would sData just point to the contents of what's returned from the function or would some space be alloctaed for sData in memory and the contents of the result of the function be copied into it?
The assignment will simply assign the sData to reference the instance returned by construct_command. No copying of data will occur.
In general, the CLR breaks the world down into 2 types
Arrays are reference types in the CLR and hence do not cause a copying of the underlying value.
Arrays are reference-types, that means that the actual array is instantiated on the heap (presumably by construct_command() ) and the function returns a reference to the array, and it is stored in (the local variable) sData.
So this is not really about memory semantics (the returned value could be null) but about the copy-semantics of reference types. The situation is totally equal to, for instance:
To put it a little more bluntly: You cannot pass an array in .Net at all, you can only pass, copy and assign references to arrays.
sData
would point to the array returned byconstruct_command
.Array is a reference type hence only reference is copied. There is No content manipulation.
sData will point to the contents of what's returned from the function. Arrays in C# are reference types, which means that assigning one array from another simply copies the reference rather than allocating new data.
Assuming sData is a local variable, it will live in the stack and it will refer to the array returned by the method. The method does not return the contents of the array itself, but a reference to the array.
In .net, arrays are first class objects and all array-type variables are in fact references.