Differences between value types and reference types

Source: Internet
Author: User

Differences between value types and reference types
Differences between value types and reference types
1. Value Type and reference type

We divide the data types we learned into two sets:

Value Type
All numeric types (longint short byte ulong uint ushort sbyte decimal double float), bool, char, enumeration, and structure

Reference Type

String, array, Class

Division basis: their memory storage structure is different

2 , Value type and reference type Similarities and Differences

A. Similarities:
Whether it is a value type variable or a reference type variable, the variable is always open up in the stack space.
B. Differences:
The real values of value-type variables are directly stored in value-type variables.
The actual value (object) of the reference type is stored in the heap space. The variables of the reference type store the actual value (object) address in the space.

3. The value type and reference type have different values.

Here, we emphasize that no matter what type of variables are assigned to each other, the value of the source variable is copied in one copy and assigned to the target variable.

A. When values are assigned to each other, the value of one variable is modified without affecting the value of the other variable.

B. variables of the reference type are assigned values to each other. Two variables of the reference type actually point to the address (object) in the same heap space. Modifying one variable will affect the other variables.

Note: string is a special type of reference, which is stored in memory according to the reference type. however, this is not the case when assigning values, because of the constant character of the string.

4. Use the code to learn more about the differences between value types and reference types.

Int temp;

Temp = 100;

Console. WriteLine ("before modification, temp =" + temp );

ChangeValue (temp );

Console. WriteLine ("modified, temp =" + temp );

Student stu = newStudent () {Name = "James", Age = 30 };

Console. WriteLine ("before modification:" + stu. Name + ":" + stu. Age );

ChangeValue (stu );

Console. WriteLine ("modified:" + stu. Name + ":" + stu. Age );

Console. ReadKey ();

Static voidChangeValue (int num)

{

Num = 200;

}

Static voidChangeValue (Student stu)

{

Stu. Age = 40;

Stu. Name = "Li Si ";

}

Result:

 

5. The following figure shows the case above.

 

I believe that through this image example, you should understand the difference between the value type and the reference type!


C # for different value types and reference types, I ++ returns I and I + 1 (similar to proximity principle = ), they return different results. In fact, their values are added with 1.
The ref operation uses the address directly. When Class1_Muti (ref a, ref B) is called, the change is a, B, and Class1_Muti (a, B) is, b's [copy], which is the difference (that is why ref is fast ). Therefore, Class1_Add (I); returns I (10 ).
Although a copy is modified, the original variable is assigned at the end. Therefore, if you execute Add (int) for the first time and then Add (ref int), the result will be different.
When Class1.Muti (ref a, ref B) is executed for the first time
Return x ++ * y ++; that is, return a ++ * B ++;
A ++ returns the value of a itself, which is 2.0. Essentially, a has already been + 1, that is, a = 3.0.
B ++ returns the Bbody value 9.0. In essence, B has already been + 1, that is, B = 10.0.
A ++ * B ++ => 2.0*9.0 => 18.0
And then call:
A ++ returns the value of a itself, which is 3.0. Essentially, a has already been + 1, that is, a = 4.0.
B ++ returns the Bbody value 10.0. In essence, B has already been + 1, that is, B = 11.0.
A ++ * B ++ => 3.0*10.0 => 30.0
What is the difference between the value type and the reference type? How to judge them? First, it helps you intuitively understand that the value type is cash and can be used directly; the reference type is passbook and must be obtained from the bank first. When a value type variable is declared, the compiler allocates a space on the stack. The space corresponds to the value type variable, which stores the value of the variable. An instance of the reference type is allocated to the stack. A new instance of the reference type is created. The obtained variable value corresponds to the memory allocation address of the instance, just like your bank account.

Related Article

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.