C #-transfer of function parameters (value transfer and reference transfer)

Source: Internet
Author: User

Recently, a few enthusiasts who have just started to learn C # asked me: What is the difference between passing parameters, passing by value, and passing by reference in functions in C. To solve this problem, I wrote a simple example program to explain it. I hope I didn't confuse them. Because it is often said to others: "If you don't say it, I still understand it. If you say it, I will be confused ".
Okay. Start now.
We know that in C #, the type can be a value type (such as INT) or a reference type (such as string). Passing parameters can be passed by value or by reference. In this way, we can get the following transmission methods through a simple combination: (1) Pass the value type by value. (2) Pass the reference type by value. (3) Pass the value type by reference. (4) Pass the reference type by reference. In general, parameters are passed by value unless specific keywords (ref and out) are used. That is, a copy is passed. One advantage of transferring a copy is that it can avoid misoperation and affect the original value. The reason is that in the called function, the operation is the copy value, not the original value. Of course, transferring copies also has side effects. The most prominent reason is the performance loss caused by replication, which is particularly prominent in large value types. Why is the default behavior of C # compiler not to pass Parameters by reference? Well, I did not think deeply about this issue. I guess it's because of the security factor, that is, the original value is in case of function misoperations. This should be similar to the requirements of the C # compiler for displaying the use of keywords (ref and out) to clearly express the intention of use and avoid misoperation. Using keywords such as REF implies that the function caller knows that in the function body, a statement that modifies the original value may change the parameter value (or State ).

......

 

(1) pass value type by value
The initial value is 5. When the function is called, a copy is made to the function, so the value is 5 after the function is returned. Well, we should have created a stack chart. However, the figure is too difficult to draw. Therefore, I am lazy and use the debug monitor of vs2008 to see it:

(2) transfer value types by reference
The initial value is still 5. This time I changed the method for passing parameters-transfer by reference. This is not a copy, but the original value (address ), this is similar to that of a big-name martial artist who cannot always use a replacement. Occasionally, they still need to fight in person. It is not surprising that the value is changed to 10. As shown in the result figure, n = 10.

(3) Pass the reference type by value and pass the reference type by reference
The reason for putting these two functions together is that, as shown in the result chart, the values of the two transfer methods have been successfully modified-both functions call a function change that is auxiliary to the modification, modify the internal status, that is, the values of m_x and m_y, from 5 to 10. Er, the original values can be successfully modified. Why are there two ways? Are there any differences between them? Where are they used? To illustrate their differences, I specifically wrote two functions named other, a new point object in the function, and used the reference passed from the parameter to reference the new point object. It is worth noting that this reference is defined in the function body. Its running is shown in the box I use.
It can be clearly seen that the value can be changed through the value transfer method, but the object referenced by it cannot be changed. The reference transfer method can be used.

By the way, there is a piece of commented out code in the code that uses the out keyword. When you try to write the two together, and then compile, the C # compiler will prompt an error (error cs0663: 'foo' cannot define overloaded methods that differ only on ref and out ). The reason is that the C # compiler generates the same il code for ref and out, while there is no difference between ref and out at the CLR level. In C #, the difference between ref and out is mainly that who is responsible for initializing this parameter so that it can use the -- ref form for off-function initialization, while out is for intra-function initialization.

I hope this article will help you.

 

Link: http://www.cnblogs.com/DonLiang/archive/2008/02/16/1070717.html

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.