110,533
社区成员
发帖
与我相关
我的任务
分享
public bool CheckUser(string UserName, out string PassWord, out int State)
{
if (UserName == "mh_ma")
{
PassWord = "123";
State = 0;
}
else
{
PassWord = "";
State = 1;
}
return false;
}
这时候我们可以得到三个返回值。
ChangeType(ref Person per)
{
per=new Person();
per.Name="123";
}
ChangeType(Person per)
{
per=new Person();
per.Name="123";
}
这两个结果就不一样,我想知道这个为什么不一样,传递的都是地址,这个地址的区别
ChangeType(ref Person per)
{
per=new Person();
per.Name="123";
}
ChangeType(Person per)
{
per=new Person();
per.Name="123";
}
这两个结果就不一样,我想知道这个为什么不一样,传递的都是地址,这个地址的区别[/quote]
是不一样的
public void Go()
{
Thing x = new Animal();
Switcharoo(ref x);
Console.WriteLine(
"x is Animal : "
+ (x is Animal).ToString());
Console.WriteLine(
"x is Vegetable : "
+ (x is Vegetable).ToString());
}
public void Switcharoo(ref Thing pValue)
{
pValue = new Vegetable();
}
输出结果:
x is Animal : False
x is Vegetable : True
如果不用ref输出结果刚好相反。
是因为用了ref之后参数pValue指向栈上的变量x,执行Switcharoo方法后x的通过变量pValue指向了new Vegetable()的地址。而不用ref参数pValue只是栈上的变量x的一个复制。执行Switcharoo方法之后,pValue变成了new Vegetable()的地址,而x依然指向animal这个对象。