Let's talk about the reference transfer and value transfer of methods from an interview question.

Source: Internet
Author: User

Today, I accidentally saw such a post on the csdn Forum, that is, there was such an interview question with the following questions:


Using System;
Public class Test1
{
Public static void Main ()
{
Int num = 0;
Person p = new Person ("Li ");
A1 (p, num );
Console. WriteLine ("{0}, {1}", p. name, num );
}
Static void A1 (Person p, int num)
{
P = new Person ("Wang ");
Num = 1;
}
}
Public class Person
{
Public string name;
Public Person (string name)
{
This. name = name;
}
}
Using System;
Public class Test1
{
Public static void Main ()
{
Int num = 0;
Person p = new Person ("Li ");
A1 (p, num );
Console. WriteLine ("{0}, {1}", p. name, num );
}
Static void A1 (Person p, int num)
{
P = new Person ("Wang ");
Num = 1;
}
}
Public class Person
{
Public string name;
Public Person (string name)
{
This. name = name;
}
}


Let's talk about the results produced by the above program and what are the reasons for the results?

The Post says that ten people have been interviewed, and ten people have incorrect answers. It seems very abnormal, but to put it bluntly, here, I want to know the interviewer's understanding of the transfer of reference values;

I have previously posted a blog about the reference transfer and value transfer at: http://www.bkjia.com/kf/201201/116158.html

Today, let's talk about the above interview questions. On the one hand, we can consolidate ourselves, and on the other hand, we can make it easy for the average person to thoroughly understand the problem;


In C #, the data type is divided into the reference type and the value type. The value type is stored in the stack. The reference type is slightly more complex. The reference type is saved in two parts, the value of the reference type is stored in the managed heap, and the reference of this value is stored in the stack. The value and value reference constitute a complete reference type variable; we often use the following syntax to declare variables:

Int I = 0;
String str = "new string ";
Int I = 0;
String str = "new string ";

During the I Declaration process, the system does two things. One thing is to find a 4-byte location in the memory stack (the length of the int type is 4 bytes ), the conversion to code should be as follows: int I = new int (); the second thing is to assign 0 to I, and convert to code should be as follows: I = 0; the actual code is as follows:

Int I = new int ();
I = 0;
Int I = new int ();
I = 0;

During the 'str' statement, the system also did two things, but the two things were inconsistent. The first thing was to find two places in the memory, one place is in the managed heap, used to store the str value, and the other place is in the stack, used to point to the storage location of the managed heap; conversion to code is also a sentence: string str = new string (); the second thing is to assign the new sting string to the variable value, but the new string is recorded in the managed heap, which is different from the value type, the code to be converted should be as follows: str = "new string"; the Code is the same, but the differences in memory allocation are large:

String str = new striing ();
Str = "new string ";
String str = new striing ();
Str = "new string ";

After clarifying the value type and reference storage method in the memory, let's take a look at the parameter transfer of the method. In C #, all parameters are passed through the value, the called method obtains copies of the value. Note that the called method obtains copies of the value. That is to say, let's look at the following method:

Public void MethDouble (int I)
{
Return I * 2;
}
Public void MethDouble (int I)
{
Return I * 2;
}

The following code calls this method in a program:

Int I = 3;

MethDouble (I );

In the above Code, the system has done the following: First, copy a copy of the variable in the memory stack. The variable name of this copy is assumed to be copy_ I. After obtaining the copy, the copy copy_ I is passed to the MethDouble Method for execution. Therefore, in the above process, anything done inside the MethDouble method will not affect variable I. Let's take a look at the following method:

<Span style = "white-space: pre"> </span> public void StrDouble (string str)
{
Return str + str;
}
<Span style = "white-space: pre"> </span> public void StrDouble (string str)
{
Return str + str;
}


The following code calls this method in a program:

String str = "myString ";
StrDouble (str );
String str = "myString ";
StrDouble (str );

Similarly, in the above Code, the system has done the following: First, reference the value of str in the stack to the variable copy. This copy variable name is assumed to be copy_str, this copy is a reference, and the reference points to the address is the address of the managed heap to which the str reference points, that is, the "myString" value in the managed heap. After obtaining the copy, the copy copy_str is passed to the method for execution. Therefore, during the operation, changes made to the copy by the method will directly modify the value in the managed heap, thus affecting the str variable outside the method;

After understanding the above principles, let's take a look at the interview questions at the beginning:

Int num = 0;
Person p = new Person ("Li ");
Int num = 0;
Person p = new Person ("Li"); declares the value type variable num, referencing the Type Variable p;

A1 (p, num );
A1 (p, num); pass the copy of num and the reference copy of p to the method;

Here we may have some questions. If the reference of p is passed in, the changes made to p should affect the value in the hosting heap. Let's take a look at the implementation of Method A1.

Static void A1 (Person p, int num)
{
P = new Person ("Wang ");
Num = 1;
}
Static void A1 (Person p, int num)
{
P = new Person ("Wang ");
Num = 1;
} Note that the above method actually uses a blind eye method. I will change the method. Let's look at it again:

Static void A1 (Person person, int num)
{
Person = new Person ("Wang ");
Num = 1;
}
Static void A1 (Person person, int num)
{
Person = new Person ("Wang ");
Num = 1;
} No change was made to the previous method. p = new Person ("wang"); everyone thought the value of p in the stack had changed. Actually, p is just a copy of the stack, in method A1, p = new Person ("Wang") is used. At this time, the system does two things: one is to find a place in the hosting heap, store the value of p (actually the person of the following method), and then reference p in Method A1 to the position of the stack, at this time, any modification made to p of A1 method is the managed heap found in the first modification, because the reference in A1 method points to the change, so the Variable p in the main function points to the managed heap and the managed heap pointed to by p in the A1 method are different positions; at this time, nothing done by the A1 method will affect the Variable p in the main function;

Therefore, the result of the interview question should be: Li, 0

 


Int num = 0;
Person p = new Person ("Li ");
Int num = 0;
Person p = new Person ("Li ");

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.