C # Basic Principles & mdash; transfer and reference of the reference type

Source: Internet
Author: User

I used to write a blog, but I only set up a shelf, like taking notes. I don't have to think about it myself, and no one else can look at it. This problem has to be changed, starting from this article...

I have been preparing for an interview recently, and I am deeply impressed by the importance of my basic skills. How can I supplement short-term resources? Practice. I was very impressed when I checked my own information. This is the problem. The original question is very simple: "For methods, parameter passing is divided into two types: Value passing and ." This is not simple, but I have to expand and think about it...

The following 1 and 2 are my obscenity, but it is helpful to record some incorrect ideas and think about correction. I hope this will not mislead you. 3 if you are in trouble.

1. What is value transfer and reference transfer?

In my first understanding, the value type is not referenced at all. Can it be referenced or passed? Its value transfer and reference transfer should be the same. As for the reference type, its value transmission should be a deep copy, and the reference transmission should be a shallow copy. At first, I was quite proud of this kind of comparison. But I can only find out What a stupid idea! There is no way. If you are a non-specialist, you cannot develop a system of knowledge, and you have blind spots. But I know this is not an excuse. It's not a great supplement. It's just like a chicken.

I read the token. In fact, I don't even know it. I don't have to forget it for a long time, and then I came up with the strange idea above.

In fact, after thinking about the entire process, I passed a definition to the value transfer and reference, and here I will first move it out: (You are welcome to criticize and correct it)

Value Transfer refers to the value in the stack; reference transfer refers to the stack address.

2. Can the value transfer and reference transfer of the reference type be analogous to the deep copy and the shallow copy?

I am still in doubt about this issue, so I wrote a small Demo.

  Main(=  Student(,             Console.WriteLine(xiaohong.Name +    +          BanZheng(= =          = =  Student( name, ==  Name { ;   Age { ; 

Explanation: Xiaohong is 12 this year. But if she wants to work, her family may be difficult. 1. If she has something to do (is it still difficult to have a family relationship? Can't explain), find the BanZheng method, change her ID card to "Hongjie" 18, take the Main method for output verification, and indeed output "Hongjie" 18. Haha. However, as I understand it, if the ref is a small copy, Age should be re-baked as a value type attribute, and its value should not be returned to xiaohong, the output should be "Hongjie" 12. It seems that the relationship is awesome. 2. If she doesn't matter, she finds a fake BanJiaZheng. If the verification is still "Hongjie" 18, if I understand that the value transfer is a deep copy, the student parameter and xiaohong should be two completely different objects, and "xiaohong" 12 should be output. It seems that this Certification Authority (I understand) has water. So I checked the information...

3. reveal the secrets of value transfer and reference transfer of the reference type

Hard work, find such a good article.

  Main(=  Student(,             Console.WriteLine(xiaohong.Name +    +  BanZheng(=  Student(,  =  Student(,  Student( name, ==  Name { ;   Age { ; 

This time the verification method has been changed. In the method body, student = new Student (...). Verify it in the Main method. If the BanZheng method is called, "Hongjie" 18 is output. If the BanJiaZheng method is called, "Xiaohong" 12 is output. It seems that the relationship has always been awesome.

Draw two figures to understand:

(1) If it is BanJiaZheng, that is, the value is passed:

The stack variables xiaohong and student have the same heap address, all pointing to 10000, that is, "small red" 12. In the BanJiaZheng method, student = new Student (...), The stack address of student is changed to 10010, while the stack address of xiaohong is still 10010. So after the method is called, the object that xiaohong points to is not changed, and it is still "small red" 12.

(2) If it is BanZheng, that is, the reference is passed:

The variables xiaohong and student point to the same stack address, in student = new Student (...) Later, the stack 001 points to the new stack address 10010, and xiaohong points to the 001 stack. Therefore, after the method is called, xiaohong points to the "Hongjie" 18 object.

So far, I have concluded that the value transfer is the value in the stack, and the reference transfer is the stack address.

This time I wrote this blog very seriously. I hope it will be more popular. If you leave a message to enlighten me, you will be grateful.

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.