参考没有像我想象的那样工作

发布于 2024-11-18 12:00:43 字数 1167 浏览 2 评论 0原文

我有以下代码,当它运行时查看它时,显示初始的“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋意浓 2024-11-25 12:00:43

编辑:好的,在您的评论中看到了描述...

如果您在(例如)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.

岁月静好 2024-11-25 12:00:43

这是调试器异常,根据 Jon 和 jamietre 的评论,值会立即更改。

It's a debugger anomaly, the values change right away, as per Jon's and jamietre's comments.

烦人精 2024-11-25 12:00:43

您将 Watch 放置在表达式 myIntmyFloat 上,而不是表达式 theInttheFloat 上,因此您不再需要看到他们的价值观。 myIntmyFloat 不存在于当前范围中。

监视窗口使用表达式而不是变量

您可以在调用堆栈中备份以观察它们的值,或者对所有您关心的四个表达式

选择调用堆栈中的原始方法以查看其值

You placed a Watch on the expressions myInt and myFloat rather than the expressions theInt and theFloat, thus you no longer see their values. myInt and myFloat do not exist in the current scope.

The Watch Window uses expressions not variables

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.

Select the original method in your call stack to view their values

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文