Using system;
Using system. Collections. Generic;
Using system. text;
Using system. diagnostics;
// The difference between the value type and the reference type is tested in this program. The value type variable cannot be directly modified. The value in the memory can be modified only through its variable reference.
Namespace ref_value
{
Class Program
{
Static void main (string [] ARGs)
{
Console. writeline ("<------ value type and reference type test start ------> ");
Int A = 1;
Change ();
// Change (Ref A); // The code after adding a ref
Console. writeline ();
Int [] B = {1 };
Change (B );
Console. writeline (B [0]);
String c = "1 ";
Change (C );
Console. writeline (C );
Stringbuilder d = new stringbuilder ();
Change (d );
Console. writeline (d );
Stopwatch Sw = new stopwatch ();
Sw. Start ();
Console. writeline ("<------ value type and reference type test completed ------> ");
Console. writeline ();
Console. writeline ();
Console. writeline ("<------ stringbuilder and string test start ------> ");
Stringbuilder sb = new stringbuilder ();
For (INT I = 0; I <10000; I ++)
SB. append (I );
Console. writeline (SW. elapsedmilliseconds); // 3 millionseconds
Sw. Reset ();
Sw. Start ();
String S = "";
For (INT I = 0; I <10000; I ++)
S + = I;
Console. writeline (SW. elapsedmilliseconds); // 421 millionseconds
Console. writeline ("<------ stringbuilder and string test completed ------> ");
}
Static void change (int I) // The output result of I is 1.
// Static void change (ref int I) // The output result of I is 2, which is the code after Ref is added.
{
I ++;
}
Static void change (INT [] ARR)
{
Arr [0] ++; // 2
}
Static void change (string S)
{
S = "2"; // 1 string is a reference type, but it is special. Once created, the string content is immutable, A new memory area is allocated for each string with different content.
}
Static void change (stringbuilder SB)
{
SB. append ("2"); // 2
}
}
}
RefKeyword to pass Parameters by reference. The effect is that when the control is passed back to the call method, any changes made to the parameters in the method will be reflected in the variable. To useRefParameter, the method definition and call method must be explicitly usedRefKeyword.