This problem should be a problem that troubles beginners of any language. For this reason, I have compiled some online materials and hope to help you solve the problem.
First, there are only two data types in C #, either the value type or the reference type.
Value Type: struct (numeric type, bool type, user-defined struct type), enumeration, can be empty type
Reference Type: array, user-defined class, interface, Delegate, object, String.
On the surface, the value type directly stores its value, while the reference type stores Reference to the value.
From the memory location, the value type is stored on the stack (thread stack), and the reference type is stored on the managed stack.
(In fact, for new users, there is no need to review the stack and managed stack. You only need to know the two different regions in the memory, but in order to maintain a rigorous academic attitude, I suggest you can refer to: http://www.cnblogs.com/axzxs2001/archive/2008/08/28/1279037.html
)
The content is still obscure. The following is an example:
1 // reference type someref
2
3 class someref
4
5 {
6
7 public int32 X;
8
9}
10
11 // Value Type someval
12
13 struct someval
14
15 {
16
17 Public int32 X;
18
19}
20
21 static void valuetypedemo ()
22
23 {
24
25 someref R1 = new someref (); // allocated on the managed Stack
26
27 someval V1 = new someval (); // allocated on the stack
28
29 r1.x = 5; // pull pointer
30
31 v1.x = 5; // The actual Modification on the stack
32
33 console. writeline (r1.x); // display 5
34
35 console. writeline (v1.x); // display 5
36
37
38
39
40 // The above code is the left part of the figure, and the following code is the right part
41
42
43
44
45 someref r2 = R1; // copy reference only ()
46
47 someval v2 = V1; // allocate and copy members on the stack
48
49 r1.x = 8; // modify r1.x and r2.x
50 v1.x = 9; // modify only v1.x without modifying v2.x
51 console. writeline (r1.x); // 8
52 console. writeline (r2.x); // 8
53 console. writeline (v1.x); // 9
54 console. writeline (v2.x); // 5
55
56}
57
58
In fact, through this example, you can clearly distinguish the partition value type from the reference type. In particular, the problem of value assignment is that value type assignment is achieved by copying values, modifying the value of a new variable does not affect the original variable. the value assigned to the reference type is actually a reference to the object. modifying any value actually modifies its reference, all values pointing to the referenced variable will change.
But sometimes you will find this problem:
String S1 = "hello ";
String S2 = S1;
Console. writeline (S1); // hello
Console. writeline (S2); // hello
S1 = "goodbye ";
Console. writeline (S1); // goodbye
Console. writeline (S2); // hello
We also mentioned that the string type belongs to the reference type, and according to the characteristics of the reference type value, the value of S2 should be changed only after the S1 value is modified?
In fact, there is no conflict here, because when the S1 value is changed, a New String object is actually created, and S1 references this new String object; S2 still references the original string object. This is because the string object is constant. That is to say, once a String object is created, its value cannot be modified. Therefore, when you change the value of a string variable, only a New String object containing the modified content is created. Pay special attention to this.
Supplement:
When designing your own type, consider using the value type or reference type. In some cases, the value type has better performance than the reference type. The value type should be considered in the following situations:
(1) A type has a behavior of a primitive type, that is, it is a very simple type. No member will modify any field of the type.
(2) A type does not need to inherit from any other type or derive any other type.
(3) instances of the type are small, about 16 bytes or smaller.
(4) type instances are larger than 16 bytes, but are not transmitted as methods or return types of methods.
Reference: http://www.cnblogs.com/chenzehe/archive/2009/01/30/1381073.html
Http://blog.csdn.net/canduecho/archive/2008/01/13/2042509.aspx