首写从这字段看 ref 就是引用的意思 out当然就是输出了
public void getRefStr(ref string str) { str="hello 你好,你变成了Ref了" }
public void getOutStr(out string outStr) { outStr = "hello 你好,你是out输出的值"; }
protected void Button1_Click(object sender, EventArgs e) { string ss="你好"; getStr(ref ss); Response.Write(ss);//输出 ref引用值 } protected void Button2_Click(object sender, EventArgs e) { string Outvalue; Method(out Outvalue); Response.Write(Outvalue); //输出out值 }
下面上个图片
点击Ref按钮
点击out
看到上面的实例和代码现在就来说说 ref 和outstring
ss="你好"; //ss其实是个引用 ss--------->"你好"
getStr(ref ss); //ref ss是告诉方法 getStr(ref string str) str哥们你的引用地址为 ss 哦
public void getRefStr(ref string str)
{
str="hello 你好,你变成了Ref了" // 引用指向新
}
ref按钮里的主法
{
ss="你好";
getStr(ref ss);
Response.Write(ss);//所以当输入ss 它的值发生变化,因为它的引用地址发生了变化
}
public void getOutStr(out string outStr)
{
outStr = "hello 你好,你是out输出的值";
}
out按钮
{
string Outvalue; //没空值,空引用 Outvalue--------->Empty
Method(out Outvalue); //输入引用别人的 outStr = "hello 你好,你是out输出的值";
Response.Write(Outvalue); //输出out值 所以Outvalue的值发生变化
}
最后总结一句,不管是out 还是 ref其实都是实际引用地址发生了变化所以它的值也就发生了变化上一个简单的地址引用图
每一次引用都开辟了内存空间