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

后端 未结 7 1003
长情又很酷
长情又很酷 2021-01-19 04:12

Given the following: byte[] sData; and a function declared as private byte[] construct_command()

if I were to then assign the resul

相关标签:
7条回答
  • 2021-01-19 04:49

    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.

    0 讨论(0)
提交回复
热议问题