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

后端 未结 7 1001
长情又很酷
长情又很酷 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:32

    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.

提交回复
热议问题