C# reference types: comparison between pass by value and pass by reference

php是最好的语言
Release: 2018-08-04 14:16:00
Original
2203 people have browsed it

1, pass by value

The value is passed by value. The essence of passing reference type by value is to pass the value. When the parameter is a value type, the "value" is the instance itself, so the passed It is an instance copy and will not affect the original instance; when the parameter is a reference type, the "value" is an object reference, so what is passed is a copy of the reference address, which will change the reference point of the original object.
String is a reference type. The effect of passing string by value is the same as the effect of passing value type by value. String is special here.
When calling a method and passing parameters, the method first creates a variable in the stack according to the parameter type, and then assigns the value of the parameter to the variable. Therefore, the value type and string type transfer instance remain unchanged, and the reference type transfer address changes. But if it is passed by reference, the address is passed, and the value of the instance will change.

2, Pass by reference
Ref and out passed by reference, whether it is a value type or a reference type, passing by reference must be modified with the ref or out keyword, ref requires that the parameters before passing must be Initialization is shown first, which is not required for out. That is to say, the parameter using ref must be an actual object and cannot point to null; while the parameter using out can accept an object pointing to null, and then the object must be materialized inside the calling method.

When a value type is passed by reference, the value type will not be boxed.

Pass by reference, what is passed is not the value of the parameter itself, but the address of the parameter. If the parameter is a value type, the address of the value type is passed; if the parameter is a reference type, the address of the object reference is passed. The result of passing a reference type by reference is the same as passing by value.

int i = 100;//Ref需要显示初始化
int j;//out不需要显示初始化
Fun(ref i,out j);
void Fun(ref int i,out int j)
    {
        j = 100;//out必须完成对象的实体化
    }
Copy after login

is as follows:

private void Awake()
    {
        SenderTest st = GetComponent<SenderTest>();//实例化类的对象
        st.i = 100;
        int i = 100;
        string s = "100";
        ChangeValue(i,s, st);//值类型,引用类型,按值传递
        print("value "+i+s+st.i);//value 100 100 200
        ChangeValue(ref i, ref s, ref st);//值类型,引用类型,按引用传递
        print("ref "+i + s + st.i);//ref 200 200 200
    }
    void ChangeValue(int i, string s, SenderTest st)
    {
        i = 200;
        s = "200";
        st.i = 200;
    }
    void ChangeValue(ref int i, ref string s, ref SenderTest st)
    {
        i = 200;
        s = "200";
        st.i = 200;
    }
Copy after login

Related articles:

Writing PHP Extension using C/C

【c# tutorial 】C# data type

The above is the detailed content of C# reference types: comparison between pass by value and pass by reference. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!