C# ref out

来源:互联网 发布:js webkittransition 编辑:程序博客网 时间:2024/06/07 04:50
ref 关键字使参数按引用传递。其效果是,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。
class testref
{
private testint(int i)
{
i = 50;
}

private testrefint(ref int i)
{
i = 50;
}

private testoutint(out int i);
{
i = 50;
}

private test()
{
int itest = 1;testint(itest); //结果为itest =1
int itest = 1;testrefint(itest); //结果为itest =50
int itest = 1;testoutint(itest); //结果为itest =50int itest ; testrefint(itest); //报错 必须初始化int iitest ;testoutint(itest); //正确不须初始化
}
}
原创粉丝点击