这个我知道,比如你看这个ChangeType(ref Person per) { per=new Person(); per.Name="123"; } ChangeType(Person per) { per=new Person(); per.Name="123"; }这两个结果就不一样,我想知道这个为什么不一样,传递的都是地址,这个地址的区别 是不一样的 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这个对象。
ref是方法内部可以不赋值,out是方法内部必须赋值,否则无法编译。
使用这些修饰符通常都是int,string这些非对象类,如果是对象,根本不需要ref和out,直接内部赋值外部也会改变
{
per=new Person();
per.Name="123";
}
ChangeType(Person per)
{
per=new Person();
per.Name="123";
}这两个结果就不一样,我想知道这个为什么不一样,传递的都是地址,这个地址的区别
使用ref,必须初始化,使用out怎么着都行,但是方法里面必须初始化对吧
{
per=new Person();
per.Name="123";
}
ChangeType(Person per)
{
per=new Person();
per.Name="123";
}这两个结果就不一样,我想知道这个为什么不一样,传递的都是地址,这个地址的区别
是不一样的
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这个对象。
拙见,继续期待楼下的回答。
{
if (UserName == "mh_ma")
{
PassWord = "123";
State = 0;
}
else
{
PassWord = "";
State = 1;
}
return false;
}这时候我们可以得到三个返回值。