C/C++ 之间变量范围的差异和 C# 还是调试器中的一个怪癖?

发布于 2025-01-09 12:31:23 字数 806 浏览 0 评论 0原文

我已经通过替换第二个变量的名称解决了这个问题,因此这是一个低优先级的问题,但我想了解会发生什么。

我的 C# 代码中遇到这种情况(变量“list”的值为“V3”):

foreach (string entry in list.Split()){                   // Loop1
    ...
}

string [] list_statuses = { "V1", "V2", "V3", "V4", ...};
foreach (string entry in list_statuses) {                 // Loop2
    ...
}

我期望出现以下行为:

  1. 在 Loop1 中,变量“entry”仅获取值“V3”,循环运行一次并完成。
  2. 在 Loop2 中,变量“entry”获取值“V1”、“V2”、“V3”……,循环一直运行并结束。

重要提示:Loop1 中的变量“entry”与 Loop2 中的变量“entry”不同:
Loop1 中“entry”的范围仅限于 Loop1,而 Loop2 中使用了一个全新的同名变量。

令我惊讶的是,在调试时,在 Loop2 中该变量多次获得值“V3”,就好像 C# 没有按预期考虑该变量的范围。
在更改第二个变量的名称时,问题解决了,但我想了解这里发生了什么:
我对变量作用域的理解来自于C/C++语言。观察到的行为是错误还是 C# 和 C/C++ 之间的变量范围存在差异?

哦,万一这是调试器中的问题:我正在使用 Visual Studio Enterprise 2017,版本 15.9.44。

I have solved this issue already by replacing the second variable's name, hence this is a low priority question, but I'd like to understand what happens.

I have this situation in my C#-code (value of the variable 'list' is "V3"):

foreach (string entry in list.Split()){                   // Loop1
    ...
}

string [] list_statuses = { "V1", "V2", "V3", "V4", ...};
foreach (string entry in list_statuses) {                 // Loop2
    ...
}

I was expecting the following behaviour:

  1. In Loop1, the variable 'entry' only gets value "V3", the loop is run once and finishes.
  2. In Loop2, the variable 'entry' gets values "V1", "V2", "V3", ..., the loop is run all those times and finishes.

Important remark: the variable 'entry' in Loop1 is not the same than the variable 'entry' in Loop2:
The scope of 'entry' in Loop1 is restricted to Loop1, and a completely new variable, allbeit with the same name, is used in Loop2.

To my surprise, while debugging this, in Loop2 that variable gets the value "V3" different times, as if C# does not take into account the scope of that variable as expected.
While changing the name of the second variable, the problem is solved, but I would like to understand what's happening here:
My understanding of variable scope comes from the C/C++ language. Is the observed behaviour a bug or is there a difference in variable scope between C# and C/C++?

Oh, in case this is a problem in the debugger: I'm working with Visual Studio Enterprise 2017, version 15.9.44.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

初见你 2025-01-16 12:31:23

这是调试器中的一个怪癖:
当我检查调试器中的值时,该值似乎是错误的。
当我在屏幕上打印它时,该值似乎是正确的。

出于显而易见的原因,我总是混淆我的代码,以隐藏我的源代码并创建一个所谓的最小且可重现的示例。但在这种情况下,这似乎是不可能的:当我在程序中插入这个问题的代码时,一切似乎都正常。

我相信这个问题可以结束,指出,如果 Visual Studio 中的 C# 调试器似乎无法在变量范围内正常工作,这可能是由于一些奇怪的调试器错误造成的。在这种情况下,我建议:

  • 要么检查真实行为(使用日志)
  • 要么使用不同的变量名称,以避免出现问题。

It's a quirk in the debugger:
When I check the value in the debugger, the value seems to be wrong.
When I print it on screen, the value seems to be correct.

For obvious reasons, I always obfuscate my code in order to hide my source code and to create a so-called minimal and reproducible example. But in this case it seems to be impossible: when I insert the code from this question in my program, everything seems to be ok.

I believe this question can be closed, stating that, in case the C# debugger in Visual Studio seems not to work correctly with variables' scope, this might be due to some weird debugger bug. In such a case, I would advise:

  • Either to check the real behaviour (using logs)
  • Either to use different variable names, in order to avoid the issue.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文