Dark Star1
Dark Star1

Reputation: 7393

What kind of memory semantics govern array assignment in c#?

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?

Upvotes: 4

Views: 998

Answers (7)

Appurist - Paul W
Appurist - Paul W

Reputation: 1458

While Rui's answer is fine overall, I do think part of it is incorrect. It doesn't make sense (hugely inconsistent) for an array to be allocated from stack memory, then accessed by reference. If that were true, the underlying memory could be freed from underneath any methods the reference was passed to.

I believe Henk Holterman described it correctly when he said very directly and unambiguously:

Arrays are reference-types, that means that the actual array is instantiated on the heap

Thank you Henk for that comment; I think it is a critical part otherwise missing from (and contradicted by) the accepted answer.

(I would have replied to a comment, but I don't have enough points to reply, and I think it's important enough to point out that the accepted answer has this problem for me to add a new comment here.) Perhaps an admin can remove the reference to "a local variable, it will live in the stack" from the answer.

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273229

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:

StreamReader reader = System.IO.File.OpenText(filename);

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.

Upvotes: 3

Dewfy
Dewfy

Reputation: 23624

Array is a reference type hence only reference is copied. There is No content manipulation.

Upvotes: 2

sepp2k
sepp2k

Reputation: 370132

sData would point to the array returned by construct_command.

Upvotes: 0

JaredPar
JaredPar

Reputation: 754665

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

  • Value Types: This is anything that derives from System.ValueType. Assignment between values of these types happens by value and essentially results in a copy of the values between locations
  • Reference Types: Anything else. Assignment between values of these types simply causes the location to reference a different object in memory. No copying of values occurs

Arrays are reference types in the CLR and hence do not cause a copying of the underlying value.

Upvotes: 6

user75690
user75690

Reputation:

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.

Upvotes: 2

JSBձոգչ
JSBձոգչ

Reputation: 41378

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.

Upvotes: 2

Related Questions