【发布时间】:2015-09-25 23:42:36
【问题描述】:
我看过一篇文章http://www.albahari.com/valuevsreftypes.aspx
据此,int 是值类型,form 是引用类型的示例。
Point myPoint = new Point (0, 0); // a new value-type variable
Form myForm = new Form(); // a new reference-type variable
string mystringval="test";
Test (myPoint, myForm); // Test is a method defined below
void Test (Point p, Form f)
{
p.X = 100; // No effect on MyPoint since p is a copy
f.Text = "Hello, World!"; // This will change myForm’s caption since
// myForm and f point to the same object
f = null; // No effect on myForm
}
所以不是表单变量myform,如果我将一个字符串值传递给函数test,它是否也会改变我已经在外面声明的原始值?
另外,将字符串保留为引用类型有什么好处,如果无论如何将值保存在堆栈中,并且只有引用将存储在堆中?
【问题讨论】:
-
@GrantWinney,如果字符串是不可变的,为什么不使用提供的最后一个值进行更新?我为什么要给 ref 一个引用类型?
标签: c# asp.net vb.net pass-by-reference value-type