. NET Common Sense value type and reference type

Source: Internet
Author: User

Value Type and reference type are a basic concept in. net.

During the interview

There are many misunderstandings about this concept. I often hear the following statement:

1. The difference between the two is that the value type is allocated to the stack, and the reference type is allocated to the stack.

This sentence is incorrect, at least not accurate.

2. The value type has better performance,

Consider the situation

 

Add someBackground information

Common value types include: Most native types, such as int float long and various struct defined by myself.

Common reference types include: string class arrays (including int)

STACK: indicates the execution stack.

Heap: it refers to the managed heap, that is, LOH + G0 + G1 + G2.

 

Let's take a look at the first point: the difference between the two is that the value type is allocated to the stack, and the reference type is allocated to the stack.

1. Assume that a statement in a method is Var OBJ = new object ();

First, the new object will be stored in the heap.

OBJ on the stack, its content is a pointer pointing to the new object

2. Assume that there is a statement in a method that is Var I = 1;

Here I is on the stack, and its value is 1 (INT type)

3. Value Type members in the class, for example, using the following definition

 
Public ClassClassa {Private IntI =1;}

Suppose there is a statement in a method that is Var OBJ = new classa ();

First, the new classa will be stored in the heap.

OBJ on the stack, its content is a pointer, pointing to the new classa

The member I in classa is also on the stack.

Assume that one of the other statements uses the I value of classa. I to be copied to the stack (most of the default cases)

4. Place the reference type on the stack

 Unsafe{VaROBJ =Stackalloc Int[100];}

Stackalloc is the keyword used to allocate memory on the stack.

The above four examples prove that both the reference type and value type can exist on the heap and stack.

However, most cases are case 1 and Case 2, so most reference types are on the stack, most

 

Let's take a look at the second point: the value type has better performance.

In the above case, 1, 2

A. When retrieving an object, scenario 1 first reads the OBJ value, which is an address, and then re-reads the real object of this address.

Case 2: Read the OBJ value. This is the real value, so it is faster than the data.

B. The objects in the stack are affected by GC and additional CPU resources are required. (The objects in the stack are released after the stack is released)

C. The objects in the heap need to be released after GC, so the memory is used for a long time.

Other cases:

1. Consider some situations, packing and unpacking; this is a unique operation of the value type in the stack and copy to the middle, this operation is still very resource-consuming

If you cannot avoid packing and unpacking, you must avoid using the value type.

2. each time a value type is transferred, it is a value copy. If a value type is large (such as a self-defined struct), this performance is also a problem (and the size of the stack must be considered)

Therefore, generally, only the class can be used for complex types.

3. In many cases, the reference comparison ratio is faster, because the reference comparison only needs to check whether the two addresses are equal.

The actual values must be considered for value comparison.

 

The difference between the value type and the reference type is actually from their names.

The actual value is passed when the value type is passed, which means that the original value is copied to a new position.

When the reference type is passed, the reference (Address) is passed. Here there is no copy of the original value action, so both references point to an object.

 

Ref

When no ref is passed, CLR will get a temporary variable for each parameter to store the value of the value type or pointer of the reference type.

In this case, modifying the value type or reference type does not affect the original value.

However, modifying the reference type members will affect the original values. The following two examples modify the parameter members and modify the parameters themselves.

Public class classa {public string name {Get; Set ;}} class program {static void main (string [] ARGs) {classa A = new classa (); test (); console. writeline (. name); // mark} Private Static void test (classa A) {. name = "mark ";}}
 
Public class classa {public string name {Get; Set ;}} class program {static void main (string [] ARGs) {classa A = new classa (); test (); console. writeline (. name); // Liu} Private Static void test (classa A) {A = new classa () {name = "Liu" };}} is not output here "};}}

When a parameter is passed in the case of ref, the CLR will pass the address of the original variable. If the variable is modified, the original member will be affected.

Public class classa {public string name {Get; Set ;}} class program {static void main (string [] ARGs) {classa A = new classa (); test (Ref A); console. writeline (. name); // here the output will be Liu} Private Static void test (ref classa A) {A = new classa () {name = "Liu "};}}

 

Note 1: how to determine whether an object is on a stack or a stack

All I 've talked about is theory. Here is verification.

Use windbg + SOS to attach to A. netProgramAnd view the situation in the heap;

The specific instructions are not mentioned. Let's take a look at the help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.