Value type and reference type parameters
1, value type and reference type
When the value type is used as a parameter, the copy of the value of the parameter is passed, and the method of the call does not change the original parameters. When the reference type is used as a parameter, it is passed to the referenced copy, the method of calling to the same data is obtained.
example:
Class SomeClass
{
Public int Changeint (int Val)
{
Return Val * 2;
}
}
Class Valreftest
{
Static void main (string [] args)
{
Someclass sc = new someclass ();
INT VAL1 = 3;
INT VAL2 = sc.changeint (VAL1);
Console.writeline ("VAL1 = {0}, VAL2 = {1}",
VAL1, VAL2);
}
}
The output is: var1 = 3, var2 = 6
Class AnotherClass
{
Public int ID;
}
Class SomeClass
{
Public AnotherClass ChangeObject (AnotherClass Ref1)
{
REF1.ID = ref1.id * 2;
Return REF1;
}
}
Class Valreftest
{
Static void main (string [] args)
{
Someclass sc = new someclass ();
Anotherclass ref1 = new anotherclass ();
REF1.ID = 3;
Anotherclass Ref2 = sc.changeObject (REF1);
Console.writeline ("ref1.id = {0}, ref2.id = {1}",
Ref1.id, ref2.id;
}
}
Output results: ref1.id = 6, ref2.id = 6
2, Ref
Using REF, you must initialize before passing the parameters, the code is as follows:
Class SomeClass
{
Public Void Changeint (Ref Int Val)
{
VAR = VAL * 2;
}
}
Class Valreftest
{
Static void main (string [] args)
{
Someclass sc = new someclass ();
INT VAL1 = 3;
Sc.changeint (Ref Val1);
Console.writeLine ("VAL1 = {0}", VAL1);
}
}
The output is: var1 = 6
3, OUT
Using OUT must be assigned to the parameters in this method without having to initialize before passing parameters.
Class SomeClass
{
Public Void Changeint (Out Int Val)
{
VAR = 2;
}
}
Class Valreftest
{
Static void main (string [] args)
{
Someclass sc = new someclass ();
Int Val1;
Sc.changeint (OUT VAL1);
Console.writeLine ("VAL1 = {0}", VAL1);
}
}
The output result is: var1 = 2
4, REF and reference types use REF to pass the reference type and use REF to transfer value type.
Class AnotherClass
{
Public int ID;
}
Class SomeClass
{
// public anotherclass changeObject (AnotherClass Ref1)
Public AnotherClass ChangeObject (Ref Anotherclass Ref1)
{
REF1.ID = ref1.id * 2;
Return REF1;
}
}
Class Valreftest
{
Static void main (string [] args)
{
Someclass sc = new someclass ();
Anotherclass ref1 = new anotherclass ();
REF1.ID = 3;
// annotherclass ref2 = sc.changeObject (Ref1);
AnotherClass Ref2 = sc.changeObject (Ref1);
Console.writeline ("ref1.id = {0}, ref2.id = {1}",
Ref1.id, ref2.id;
}
}
5, OUT and reference type
Using the OUT passed the reference type, the parameter must be assigned to the parameter inside the method, notice that the field assignment to the parameters.
Class AnotherClass
{
Public int ID;
}
Class SomeClass
{
// this Won't compile
Public Anotherclass ChangeObject (Out ANOTHERCLASS REF1)
{
REF1.ID = ref1.id * 2;
Return REF1;
}
// this Won't compile
Public Anotherclass ChangeObject (Out ANOTHERCLASS REF1)
{
REF1.ID = 4;
Return REF1;
}
// this Won't compile
Public Anotherclass ChangeObject (Out ANOTHERCLASS REF1)
{
INT x = ref1.id;
Ref1 = new annotherclass ();
REF1.ID = x * 2;
Return REF1;
}
// this Will Compile
Public Anotherclass ChangeObject (Out ANOTHERCLASS REF1)
{
Ref1 = new annotherclass ();
REF1.ID = 99;
Return REF1;
}
}
Class Refbyouttest
{
Static void main (string [] args)
{
Someclass sc = new someclass ();
Anotherclass ref1 = new anotherclass ();
REF1.ID = 3;
AnotherClass Ref2 = sc.changeObject (OUT REF1);
Console.writeline ("ref1.id = {0}, ref2.id = {1}",
Ref1.id, ref2.id;
}
}
The output is: ref1.id = 99, ref2.id = 99