【发布时间】:2011-07-07 00:02:17
【问题描述】:
所以字符串是引用类型对吧?我的理解是即使将字符串 ByVal 传递给方法时,也会传递对堆中字符串的引用。
呜呜呜……
String myTestValue = "NotModified";
TestMethod(myTestValue);
System.Diagnostics.Debug.Write(myTestValue); /* myTestValue = "NotModified" WTF? */
private void TestMethod(String Value)
{
Value = "test1";
}
或者
Dim myTestValue As String = "NotModified"
TestMethod(myTestValue)
Debug.Print(myTestValue) /* myTestValue = "NotModified" WTF? */
Private Sub TestMethod(ByVal Value As String)
Value = "test1"
End Sub
我错过了什么?引擎盖下发生了什么?我敢打赌价值会改变....
【问题讨论】:
-
我推荐阅读这篇文章:yoda.arachsys.com/csharp/parameters.html 以及重复问题的答案。
-
哇,我一点也不明白。
myTestValue怎么可能改变呢?不是在修改吗?你认为你到底是如何修改它的? -
@Stan:人们非常(非常)经常混淆按引用传递和按值传递引用类型。
标签: c# vb.net reference-type byref byval