C # ref type and Value Type

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.