Different Storage:
The value type is stored in the stack, and the reference type is stored in the managed stack.
Value Type example:
I = 20;
J = I;
The above statement stores 20 values in two places of memory;
Example of reference type:
class Vector { public long value { get; set; } } class Program { static void Main(string[] args) { Vector x, y; x = new Vector(); x.value = 30; y = x; Console.WriteLine(y.value); y.value = 50; Console.WriteLine(x.value); Console.ReadKey(); } }
View code
The above code, X value assignment, Y value also changes, Y value assignment, X value also changes, the result is 30 and 50
Different reference types: String
String S1, S2;
S1 = "123 ";
S2 = S1;
Console. writeline ("S1:" + S1 );
Console. writeline ("S2:" + S2 );
S1 = "456 ";
Console. writeline ("S1:" + S1 );
Console. writeline ("S2:" + S2 );
Console. readkey ();
Result: S1: 123
S2. 123
S1. 456
S2. 123
The result shows that changing the S1 value has no effect on S2 because the string cannot be changed. modifying a string creates a new String object.
The other string will not change.
Summary: In C #, the basic data types such as bool and long are both value types. If you declare a bool variable and assign it to the value of another bool variable,
There will be two bool values in the memory. If you change the first bool value in the future, the value of the second bool variable will not change.
If it is defined as a value type, it should be declared as a structure.
C # value type and reference type