Value Type and reference type
In c # built-in basic types, all types except object and string are reference types. At the same time, the classes defined by myself are also reference types. Because all custom classes are inherited from System. Object.
For more information, see Stack, heap, Value types, reference types, boxing and Unboxing.
Value Transfer and reference Transfer
Create a console program file: ValueAndReference. cs
Code
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace CSType
{
Public class ValueAndReference
{
Public ValueAndReference ()
{
TestClass tc = new TestClass ();
ChangeValue (tc. ps); // value transfer
Console. WriteLine ("tc. ps value:" + tc. ps );
// Output: Gossip Girl, oh!
ChangeValue (ref tc. ps); // use the ref key to force reference Transfer
Console. WriteLine ("ref tc. ps value:" + tc. ps );
// Output: Gossip Girl, oh! Was Changed
ChangeObject (tc); // The class is passed by reference by default.
Console. WriteLine ("tc. ps value:" + tc. ps );
// Output: Hello world
String s = ", ";
ChangeValue (s); // value transfer
Console. WriteLine ("s value:" + s );
// Output: pirate ship Length
ChangeValue (ref s); // use the ref key to force the reference to be passed
Console. WriteLine ("s value:" + s );
// Output: pirate ship length, wwas Changed
Console. ReadLine ();
}
Public void ChangeValue (string s)
{
S = s + "was Changed ";
}
Public void ChangeValue (ref string s)
{
S = s + "was Changed ";
}
Public void ChangeObject (TestClass o)
{
O. ps = "Hello world ";
}
}
Public class TestClass
{
Public string ps = "Gossip Girl, oh! ";
Public TestClass ()
{
}
}
}
Let's analyze:
1. Class TestClass is a helper class that assists us in testing.
2. There are three methods in the ValueAndReference class: ChangeValue and ChangeObject. The method name does not matter. You can write ChangeObject123 and ChangeObjectsdfsdf for use.
3. When the value type variables in C # are passed, all values are passed.
C # When the reference type variables are passed, all are passed by reference. Except string. For example:
String s = ", ";
ChangeValue (s); // value transfer
Console. WriteLine ("s value:" + s );
// Output: pirate ship Length
The variable s here is a reference type, but it is passed as a value.
4. value transfer:
String s = ", ";
ChangeValue (s); // value transfer
Console. WriteLine ("s value:" + s );
// Output: pirate ship Length
The scope of the s variable is not in the ChangeValue method. We use the parameter method to pass s in. Note that the value of s variable is passed here. It is not the address of the s variable in the memory. The value of the s variable is copied to ChangeValue. Although the value of the s variable is changed within ChangeValue, ChangeValue (s) cannot change the value of the external s variable.
5. transfer a reference:
ChangeValue (ref s); // use the ref key to force the reference to be passed
Console. WriteLine ("s value:" + s );
At this time, the address of the s variable in the memory is passed, not the value copy. therefore, when we modify the value pointed to by the memory address, the value of the external s variable will be modified. Because the s Variable inside and outside the method is a memory address, pointing to the same value.
======================================
Common examples:
Suppose I have 100 RMB (external s variable). Now I will give you 100 RMB (Transfer s variable), but instead of directly giving it to you, I will copy the 100 in my hand, then we will give you 100 (Transfer Method: Value Transfer). After you buy something, there will be 40 (ChangeValue (string s) function functions left ), but I still haven't changed my 100 (the value of the external s variable has not changed ).
Again, I have $100 in my wallet (external s variable ). I will tell you where you put your wallet. You can directly get $100 in your wallet (Transfer Method: Reference transfer, that is, transfer the memory address). After you buy something, there are 40 left, then you put the remaining money back into your wallet (the function of the ChangeValue (ref string s) function), and my money is only RMB 40 (the value of the external s variable is changed)