C # reference transfer and value transfer

Source: Internet
Author: User

We know that the data types of c # are divided into value type and reference type. Value types include the basic type (numeric type, bool type, etc.), structure, and enumeration. reference types include class, delegate, and array. When using methods to pass variables, you need to understand the value type and reference type as the parameter transmission mechanism.

Let's take a look at the following code:



Namespace ConsoleApplication1
{
Class Program
{
Static void Main (string [] args)
{
Int aNumber = 10;
A a = new ();
A. Name = "aaaaaaaa ";
Console. WriteLine (aNumber );
Console. WriteLine (a. Name );
ToDouble (aNumber );
ToDouble ();
Console. WriteLine (aNumber );
Console. WriteLine (a. Name );
Console. ReadKey ();
}
Static void ToDouble (int number)
{
Number * = 2;
}
Static void ToDouble (A)
{
A. Name + = "ssssssss ";
}
}
Class
{
Private string name;
Public string Name
{
Get {return name ;}
Set {name = value ;}
}
}
}
 

Output result:

10

10

10

1010

The result shows that the value type aNumber has not changed after function processing, but the attribute of referenced class A has changed. Why does this happen?

When the data type is stored in memory, the value type is stored in the stack, the reference type stores its value in the managed stack, and the stack stores a reference to the stack address, when passing parameters in a method, all types are the locations in the stack. The specific process is to copy a passed object on the stack and then pass the object to the parameter, the value type is stored in the stack. When passed as a parameter, a copy of the value is directly copied to the method. Any operation performed by the method on the copy will not affect the original value of the value, however, the reference type copies the stack reference, which still points to the stack reference type. Therefore, operations on the application type will change.

Next let's look at a piece of code:



Namespace ConsoleApplication1
{
Class Program
{
Static void Main (string [] args)
{
String aStr = "10 ";
Console. WriteLine (aStr );
ToDouble (aStr );
Console. WriteLine (aStr );
Console. ReadKey ();
}
Static void ToDouble (string aStr)
{
AStr + = aStr;
}
}
}
 

Output result:

10

10

The string type is a reference type, so the method still transmits a copy of the string variable reference in the stack. But why didn't the above Code change the value of the string variable? Here we need to know that in c #, string is a special type, and Microsoft does not advocate the following code:



Namespace ConsoleApplication1
{
Class Program
{
Static void Main (string [] args)
{
String aStr = "aaaaa ";
String aOtherStr = "bbbbb ";
String theyConcat = aStr + aotherStr;
}
}
}
 

The above code first declares an aStr variable and connects it to another string. In the specific implementation process, the system first saves the aaaaa value in the hosting center. During the connection operation, copy the value of aaaaa first, connect bbbbb later, and assign the address of the new value to aStr. Therefore, aaaaa is not deleted in the memory, until the Garbage Collector recycles it. A good practice is to use the StringBulider class for operations when you need to connect strings. It will directly connect to the end of the object without replicating the value first.

Return to the above method. When the String type is passed as a parameter, the system will first copy the value in the managed heap, and the address obtained on the stack points to the new value, therefore, the operation of the method on the string type does not affect its original value.

Here we will inevitably think, what if I need to use a method to change the value type value? For example, when we need a method to output multiple values, we know that when a method has a return value, the return value always has only one value, what if I want to input the method with parameters a and B and get two results at the same time? In this case, you need to use the reference to pass. The c # method uses the ref keyword, which indicates that the parameter is passed as the reference type. See the following code:



Class Program
{
Static void Main (string [] args)
{
Int a = 10;
Int B = 50;
Console. WriteLine ();
Console. WriteLine (B );
ToDouble (ref a, ref B );
Console. WriteLine ();
Console. WriteLine (B );
Console. ReadKey ();
}
Static void ToDouble (ref int a, ref int B)
{
A * = 2;
B * = 2;
}
}
 

Output result:

10

50

20

100

Through this method, we change the values of a and B at the same time.

From the column of luxin
 

Related Article

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.