类的方法(通过引用来传递参数)
class zoo//定义一个类
{
private int streetNumber = 123;
private string StreetName = "Beijing Road";
public void GetAddress(ref int number, ref string name)//注意关键字ref
{
number = streetNumber;
name =StreetName;
}
}
streetNumber has changed!
123
{
private int streetNumber = 123;
private string StreetName = "Beijing Road";
public void GetAddress(ref int number, ref string name)//注意关键字ref
{
number = streetNumber;
name =StreetName;
}
}
zoo myzoo = new zoo();
int zooStreetNumber = 0;
string zooStreetName = null;
myzoo.GetAddress(ref zooStreetNumber,ref zooStreetName); //通过引用传递,实际上传递的是地址。而值传递,方法使用的是变量值的拷贝。
if (zooStreetNumber != 0)
{ Response.Write("streetNumber has changed!<br>");
Response.Write(zooStreetNumber);
}
streetNumber has changed!
123