C#Out/Ref 关键字用法

C#编程 · 01-02 · 381 人浏览
都可以改变外部变量的值
void jiafa(int a, int b, ref int c)
{
    c = a + b;
}

// Start is called before the first frame update
void Start()
{
    //ref修饰的必须在外面赋值
    int res=0;
    jiafa(1, 2,ref res);
    print(res);
}

void jiafa(int a, int b, out int c)
{
    c = a + b;
}

// Start is called before the first frame update
void Start()
{
    //out修饰的不用在外面赋值
    int res;
    jiafa(1, 2, out res);
    print(res);
}
  1. 111 01-03

    111

Theme Jasmine by Kent Liao