参考没有像我想象的那样工作
我有以下代码,当它运行时查看它时,显示初始的“myInt”和“myFloat”在方法调用返回之前不会更改它们的值。由于它们每次都作为“ref”传递,因此每次在调用的方法中更改它们的值时,它们的值不应该改变吗?
class Tester
{
public void Run()
{
int myInt = 42;
float myFloat = 9.685f;
Console.WriteLine("Before starting: \n value of myInt: {0} \n value of myFloat: {1}", myInt, myFloat);
// pass the variables by reference
Multiply( ref myInt, ref myFloat );
Console.WriteLine("After finishing: \n value of myInt: {0} \n value of myFloat: {1}", myInt, myFloat);
}
private static void Multiply (ref int theInt, ref float theFloat)
{
theInt = theInt * 2;
theFloat = theFloat *2;
Divide( ref theInt, ref theFloat);
}
private static void Divide (ref int theInt, ref float theFloat)
{
theInt = theInt / 3;
theFloat = theFloat / 3;
Add(ref theInt, ref theFloat);
}
public static void Add(ref int theInt, ref float theFloat)
{
theInt = theInt + theInt;
theFloat = theFloat + theFloat;
}
static void Main()
{
Tester t = new Tester();
t.Run();
}
}
I have the following code which when looking at it while it's running shows that the initial 'myInt' and 'myFloat' do not change their values until the method call returns back. Shouldn't their values change each time they are altered within the called methods since they are passed as 'ref' each time?
class Tester
{
public void Run()
{
int myInt = 42;
float myFloat = 9.685f;
Console.WriteLine("Before starting: \n value of myInt: {0} \n value of myFloat: {1}", myInt, myFloat);
// pass the variables by reference
Multiply( ref myInt, ref myFloat );
Console.WriteLine("After finishing: \n value of myInt: {0} \n value of myFloat: {1}", myInt, myFloat);
}
private static void Multiply (ref int theInt, ref float theFloat)
{
theInt = theInt * 2;
theFloat = theFloat *2;
Divide( ref theInt, ref theFloat);
}
private static void Divide (ref int theInt, ref float theFloat)
{
theInt = theInt / 3;
theFloat = theFloat / 3;
Add(ref theInt, ref theFloat);
}
public static void Add(ref int theInt, ref float theFloat)
{
theInt = theInt + theInt;
theFloat = theFloat + theFloat;
}
static void Main()
{
Tester t = new Tester();
t.Run();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:好的,在您的评论中看到了描述...
如果您在(例如)
Add
中放置了一个断点,那么您的监视变量将不会更改,除非您重新评估它们 - 这必须在正确的堆栈中完成。当断点被击中时,转到“调用堆栈”视图,双击“运行”方法(这不会改变您必须到达的位置,只是改变您正在查看的堆栈帧),您将看到值更新。EDIT: Okay, having seen the description in your comments...
If you put a breakpoint in (say)
Add
then your watch variables won't change unless you get them to be re-evaluated - which has to be done in the right stack from. When the breakpoint has been hit, go to the Call Stack view, double click on the "Run" method (which doesn't change where you've got to, just which stack frame you're looking at) and you'll see the values update.这是调试器异常,根据 Jon 和 jamietre 的评论,值会立即更改。
It's a debugger anomaly, the values change right away, as per Jon's and jamietre's comments.
您将 Watch 放置在表达式
myInt
和myFloat
上,而不是表达式theInt
和theFloat
上,因此您不再需要看到他们的价值观。myInt
和myFloat
不存在于当前范围中。您可以在调用堆栈中备份以观察它们的值,或者对所有您关心的四个表达式。
You placed a Watch on the expressions
myInt
andmyFloat
rather than the expressionstheInt
andtheFloat
, thus you no longer see their values.myInt
andmyFloat
do not exist in the current scope.You can back up in the call stack to observe their values, or place a watch on all four of the expressions you care about.