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);
//正确不须初始化
}
}
C# ref out
最新推荐文章于 2025-04-22 22:17:34 发布