C# 和 VB.NET 中的直接窗口行为差异

发布于 2024-12-16 02:20:14 字数 537 浏览 2 评论 0原文

我注意到,在调试 C# 项目和 VB.NET 项目时,VS 2010 中的即时窗口的行为有所不同,尽管我无法找到有关此差异的任何具体文档。

对于 C# 项目,我可以简单地输入任何表达式,它将被计算并显​​示,即输入

foo.bar ==“baz”

将输出

然而,在 VB.NET 中,执行相同的操作不会产生任何结果。

我必须在表达式前面加一个问号才能使其起作用。

?foo.bar = "baz"

为清楚起见进行编辑,并编辑上面的错误示例:

所有其他表达式都表现出相同的行为,包括简单的数学,例如“1 + 2”。但有时错误消息会有所不同,因为 1 + 2 会导致错误“数字标签后面必须跟冒号”。

有没有一种方法可以“修复”此行为并使 VB.NET 即时窗口的行为更像 C# 窗口?必须输入一个?经常使用它时,在每个语句前面可能会很痛苦。

I have noticed that the immediate window in VS 2010 behaves differently when debugging a C# project and a VB.NET project, although I haven't been able to find any specific documentation of this difference.

For C# projects, I can simply type in any expression, and it will be evaluated and displayed, i.e. typing in

foo.bar == "baz"

will output

false

In VB.NET, however, doing the same thing outputs nothing.

I have to put a question mark in front of the expression for it to work.

?foo.bar = "baz"

false

Edit for clarity and my bad example above:

All other expressions exhibit the same behavior, including simple math such as '1 + 2'. Sometimes the error message is different though, as 1 + 2 results in the error 'Labels that are numbers must be followed by colons.'

Is there a way to 'fix' this behavior and make the VB.NET immediate window behave more like the C# one? Having to type a ? in front of every statement can be a pain when using it frequently.

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

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

发布评论

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

评论(2

好听的两个字的网名 2024-12-23 02:20:15

直接窗口的语义只是不同。在 C# 中,您输入的任何表达式或语句都会被求值,并且求值结果会打印到窗口中。在VB.NET中,你必须输入完整的语句;您不能输入纯粹的表达式。在您的示例中,正如您所发现的,如果您想将任何内容打印到窗口,则需要使用“Print”语句(别名为 ?)。

造成这种情况的原因之一是语言的语义不同。正如 Bob Kaufman 提到的,= 可以是赋值运算符或相等测试。如果 VB.NET 窗口像 C# 窗口一样工作,则无法确定 a = b 是否意味着“将 b 分配给 a”或“评估 b 是否等于 a”。

VB.NET 中的赋值没有值; a = b = 4 表示“评估 b 是否等于 4,并将评估结果赋给 a”。这意味着 a 将等于 true 或 false。

在 C# 中,赋值也是一个带有值的表达式,因此 a = b = 4 的意思是“将值 4 赋给 b,并将表达式 (b = 4) 的值赋给 a。 ”这意味着 a 将等于 4。

The semantics of the immediate windows are just different. In C#, any expression or statement you enter is evaluated, and the result of the evaluation is printed to the window. In VB.NET, you have to enter a complete statement; you can't enter a bare expression. In your example, as you discovered, you need to use the 'Print' statement (the alias for which is ?) if you want to print anything to the window.

One reason for this is that the semantics of the languages are different. As Bob Kaufman mentioned, = can be an assignment operator or an equality test. If the VB.NET window worked like the c# window, there would be no way to determine whether a = b meant "assign b to a" or "evaluate whether b is equal to a".

Assignments do not have a value in VB.NET; a = b = 4 means "evaluate whether b is equal to 4, and assign the result of that evaluation to a." This means that a will either be equal to true or false.

In C#, an assigment is also an expression with a value, so a = b = 4 means "assign the value 4 to b, and assign the value of the expression (b = 4) to a." This means that a will be equal to 4.

空袭的梦i 2024-12-23 02:20:15

如果您不使用 ?,立即窗口解析器需要一个语句命令。该命令

foo.bar = "baz"

在vb.net中是合法的,它是一个赋值语句,为对象foo的bar字段或属性赋予值“baz”。然而,如果 bar 是该类的方法,它就会抱怨。同样,“1+2”在 vb.net 中也不是有效的语句,?命令可以帮助解释器理解您想要计算表达式。要将 = 运算符从赋值运算符转换为比较运算符,必​​须使解析器了解正在计算表达式。 ?必需的。对于“1+2”也是如此,vb.net 语句解析器接受语句开头的数字作为语句标签,适合 GoTo。

C# 语言遵循大括号语言标准,其中任何表达式也是有效的语句。因此,“1+2”在没有 ? 帮助的情况下被解释为有效的陈述。这也是它需要一个单独的符号来表示相等运算符 (==) 的原因,否则解析器将无法知道赋值语句和表达式之间的区别。

The immediate window parser expects a statement if you don't use the ? command. The command

foo.bar = "baz"

is legal in vb.net, it is an assignment statement, giving the bar field or property of the object foo the value "baz". It is however going to complain if bar is a method of the class. Similarly, "1+2" is not a valid statement in vb.net, the ? command helps the interpreter to understand that you meant to evaluate an expression. To turn the = operator from an assignment into a comparison operator, you have to make the parser understand that an expression is being evaluated. ? required. Same thing for "1+2", the vb.net statement parser accepts a number at the start of a statement as a statement label, fit for a GoTo.

The C# language follows the curly brace languages standard where any expression is also a valid statement. So "1+2" is interpreted as a valid statement without help from ? Which is also the reason it needs a separate symbol for the equality operator (==), a parser wouldn't otherwise know the difference between an assignment statement and an expression.

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