Unity Learning (II) ref and out parameters

ref parameter

Use ref parameters, the method is performed after the completion, the change in the parameter to reflect the variables

 

test:

public class TestRef : MonoBehaviour
{
    private void Start()
    {
        int num1 = 0;
        int num2 = 0;
        Test1(ref num1, num2);
        Debug.Log("num1:" + num1 + "\nnum2:" + num2);
    }

    private void Test1(ref int num1, int num2)
    {
        num1 = 10;
        num2 = 10;
    }
}

 

Output:

 

Note :

 A: Before you pass parameters to be assigned to the parameter.

 Two: When you call a method, you must add ref keyword.

 

out parameters

Use out parameters, after the implementation of the method, the value of the parameter will be affected by the method of

 

test:

  

    private void Start()
    {
        int num;
        Test2(out num);
        Debug.Log("num:" + num);
    }

    private void Test2(out int num)
    {
        num = 10;
    }

 

result:

 

Note:

 In the former method calls, you can not assign parameters

 Within a method, must out assignments modified parameters

Guess you like

Origin www.cnblogs.com/CCLi/p/12626460.html