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

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

    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.

提交回复
热议问题