The pit that C# copy array is easy to step on--reference type and value type

  I have used a lot of arrays in my recent projects, and in the process of using it, I have encountered a relatively low-level problem: how to assign the value of one array to another array? (Pass by value? Pass by reference?) The application scenario at that time required the author to copy the array by value transfer. The author naively typed: int[] a = b; Although it passed the compilation, the result was not as I expected. (The author expects to modify another array without affecting the source array) So I read dalao's blog and official documents, and found the reason: the array is a reference type, and direct copying is equivalent to directly copying the address of each element. Therefore, with the mentality of avoiding repeating mistakes, hereby excerpted:

   OK, before entering the text, go to the two official document portals:

  Simply extract the content of the portal above

There are two types in C#: reference types and value types. A variable of reference type stores a reference to its data (object), while a variable of value type contains its data directly. With reference types, two variables can refer to the same object; therefore, operations performed on one variable affect the object referenced by the other variable. For value types, each variable has its own copy of the data, and operations performed on one variable do not affect the other variable (except for in, ref, and out parameter variables; see in, ref, and out parameter modifiers).

  After a brief overview of reference types and value types, I flipped through the documentation on the two types of variable storage and found something like this:

Reference types: Reference types are stored in the heap (managed heap Heap). When a type is instantiated, a part of the heap is allocated to store the instance of the class. References to class objects are still stored on the stack.

Value type: The value type is always allocated where it is declared. When it is used as a local variable, it is stored on the stack (Stack); when it is a field of a class object, it is stored in the heap following this class.

OK, let's briefly discuss the issues discussed by dalao on StackOverFlow:

Stack vs Heap

  The general idea is: Where is the allocated space for the above variable description?

  Then we go to the example of dalao below (I think the description is really fried chicken!):

Answer

  The above answer roughly describes: the value type in the method body is usually stored in the stack (that is, what we commonly call the stack), while the reference type variable and the value type variable it contains are stored together in the managed heap (not the commonly known heap! No!), OK and another example from dalao:

Definition of dalao

The following is an understanding diagram of dalao for the corresponding code:

Enter image description

Enter image description

Here are the concepts of stack and managed heap:

Managed heap:

The managed heap is different from the heap. It is managed by the CLR (Common Language Runtime). When the heap is full, it will automatically clean up the garbage in the heap, which brings convenience: you don't need to care about the issue of memory release.

Data structure stack: It is a last-in, first-out data structure, that is, stack memory stack: There are two storage areas (heap area, stack area) in the memory.

  • Stack area: store function parameters, local variables, return data and other values, which are automatically released by the compiler
  • Heap area: objects of reference type are stored, which are released by the CLR

Regarding this part of knowledge, there is a good blog recommendation! Portal: C# Heap & Stack

OK, back to the author's question at the beginning, what if you want to copy the array?

  1. If it is passed by reference, you can directly assign the array variable through '='
  2. If it is passed by value, you can use the Array.Copy() method or Buffer.BlockCopy() , or you can use the array variable .Clone() or Copy() method

PS: Attach a description of the default value of the value type variable: Default value table (C# reference)

OK, let's record this first!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325068708&siteId=291194637