Demo of small value type and reference type
When we use a reference type, we are actually processing the type pointer, not the type itself. When we use the value type, we are using the value type itself. Sounds confused, right?
The example is the best description.
Assume that we execute the following methods:
Public int ReturnValue ()
{
Int x = new int ();
X = 3;
Int y = new int ();
Y = x;
Y = 4;
Return x;
}
We will get the value 3, which is very simple, right?
Assume that we first use the MyInt class
Public class MyInt
{
Public int MyValue;
}
Then, execute the following method:
Public int ReturnValue2 ()
{
MyInt x = new MyInt ();
X. MyValue = 3;
MyInt y = new MyInt ();
Y = x;
Y. MyValue = 4;
Return x. MyValue;
}
What will we get ?... 4!
Why ?... How does x. MyValue become 4 ?... Let's look at what we have done and then we know what is going on:
In the first example, everything is as planned:
Public int ReturnValue ()
{
Int x = 3;
Int y = x;
Y = 4;
Return x;
}
In the second example, we do not get "3" because the variables "x" and "y" both point to the same object in the heap.
Public int ReturnValue2 ()
{
MyInt x;
X. MyValue = 3;
MyInt y;
Y = x;
Y. MyValue = 4;
Return x. MyValue;
}
We hope that the above content will give you a better understanding of the basic differences between the value type and the reference type in C #, and have a basic understanding of when pointers and pointers are used.
The author stays up late