C # Reference types: Passing by value, passing by reference

Source: Internet
Author: User
1, passing by value

Values are passed by value, the substance of the reference type by value is the pass-through value, when the argument is a value type, the "value" is the instance itself, so the instance copy is passed, the original instance is not affected; When the argument is a reference type, "value" is the object reference, so the reference address copy is passed to the original object.
String is a reference type, and string is passed by value in the same way that a value type is passed by value, and string is special here.
When an invocation method takes a parameter pass, the method creates a variable in the stack based on the parameter type, and assigns the value of the parameter to the variable. Therefore, the value type passes an instance of the string type unchanged, and the reference type passes the address change. However, if you pass by reference, it is the delivery address, and the value of the instance changes.

2, passing by reference
Ref and out passed by reference, regardless of value type or reference type, pass by reference must be decorated with ref or out keyword, ref requires that arguments before passing must first show initialization, and out is not required. That is, a parameter that uses ref must be an actual object, not NULL, and an out parameter can accept an object that points to null, and then the object must be manifested within the calling method.

Value types are not boxed for value types when they are passed by reference.

Passed by reference, not the value of the parameter itself, but the address of the parameter. If the argument is a value type, the address of the value type is passed, and if the argument is a reference type, the address of the object reference is passed, and the reference type passes the result by reference and by value by reference.

int i = 100;//ref need to display initialize int j;//out do not need to display initialization fun (ref i,out j); void fun (ref int i,out int J)    {        J = 100;//out must complete the object's entity    }

As follows:

private void Awake ()    {        Sendertest st = getcomponent<sendertest> ();//The object of the instantiated class        st.i =;        int i = +;        string s = "n";        ChangeValue (I,s, ST);//value type, reference type, pass print by value        ("value" +i+s+st.i),//value,        changevalue (ref I, ref s, ref ST);//value type, reference type, pass print by reference        ("ref" +i + S + st.i);//ref (+),    changevalue (int i, string s, Sendert EST st)    {        i = $;        s = "$";        ST.I = $;    }    void ChangeValue (ref int I, ref string S, ref sendertest St)    {        i = n;        s = "$";        ST.I = $;    }

Related articles:

Writing PHP Extension in C + +

C # Tutorial C # data type

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.