Comparison and Analysis of instances c # usage of referenced Parameters

Source: Internet
Author: User

C # in-depth analysis on the transmission of referenced Parameters
Value-type variables store data, while reference-type variables store references to actual data. (This is very important. After understanding it, we can differentiate between value types and reference types)

When passing a parameter, the value type is passed in the form of a value (passing a value does not affect the variable itself). It is the form parameter that will be copied to the function, therefore, any changes made to the parameter in the function body class will not affect the original value;

The reference type is passed in the form of object references (the reference is passed, that is, the same parent level is passed and the same parent level is owned ), the reference of the object to be passed is copied to the parameter of the function. In this case, the reference is copied. Note: The reference is copied, not the original reference. It points to the same object as the original reference, therefore, changes made to the referenced object will directly affect the original value, but for the reference itself, any changes in the function will not affect the original reference.

First, we will list two examples of previous generations:

     data=                            a1.data = ; a1 =  A();  a1.data = ;    A a =  A(); F(a);  Console.WriteLine(a.data);        }    }

This is a direct transfer example. When F (a) is called, the value is passed over, and a is a reference parameter, so the reference is passed, that is to say, the reference of a is passed to the a1 in the F () function, and the a1 operation in the F () function: a1.data = "2"; changes the value of, when a1 = new A (); is executed, a1 references are initialized, that is, it is no longer the passed one. Therefore, executing a1.data = "3"; has no effect on. In the following example, you will see that the reference is passed instead of A value, that is, the reference is passed to a1. When a1 = new A ();, the reference is changed, that is to say, the referenced father has changed itself, that is, the reference of a has changed, and the value of a has changed to 3.

 

Ref string parameter: the ref keyword allows the parameter to be passed 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 use the ref parameter, you must explicitly use the ref keyword for both method definition and call methods. For example, for the value type, it can be passed as the above referenced string parameter. For a parameter already of the reference type, will it be said that this is not an additional step? Actually, it is not because the actual mechanism is completely different: Examine the variants of the previous example.

     data=      F(     a1.data = ;  a1 =  A();    a1.data = ;  A a =  A();  F( a);  Console.WriteLine(a.data);  }    

It can be understood that the parameter transfer of the referenced object without ref is equivalent to the normal pointer transfer in c ++ (function declaration is equivalent to void F (Type * v )), when there is a ref, the parameter passing of the referenced object is equivalent to passing a pointer to a pointer in c ++ (function declaration is equivalent to void F (Type ** v )).

Http://www.cnblogs.com/roucheng/

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.