如何使用 Visual Studio 中的立即窗口?
立即窗口是一个非常有用的调试应用程序的工具。 它可用于执行在断点上下文中有效的代码语句并检查值。 我还用它来输入代码片段来学习语言功能。
如何使用立即窗口?
The Immediate Window is an immensely useful tool for debugging applications. It can be used to execute code statements that are valid in the context of a break point and inspect values. I also use it to type code snippets to learn language features.
How do you use the Immediate Window?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Visual Studio 中的立即窗口的一个很好的功能是它能够评估方法的返回值,特别是当它被客户端代码调用但它不是变量赋值的一部分时。 如前所述,在调试模式下,您可以与变量交互并在内存中执行表达式,这在执行此操作方面发挥着重要作用。
例如,如果您有一个返回两个数字之和的静态方法,例如:
然后在立即窗口中您可以键入以下内容:
如您所见,这对于静态方法非常有效。 但是,如果该方法是非静态的,那么您需要与该方法所属的对象的引用进行交互。
例如,假设您的类如下所示:
如果该对象已存在于内存中并且在作用域内,那么只要它在您的调用之前被实例化,您就可以在立即窗口中调用它。当前断点(或者至少在调试模式下代码暂停的任何地方之前):
此外,如果您想直接交互和测试方法而不依赖于内存中的现有实例,那么您可以实例化您自己的断点 立即窗口中的实例:
如果您想进行进一步的评估、计算等,您可以更进一步,暂时将方法的结果分配给变量:
此外,如果您甚至不想声明一个新对象的变量名称,并且只想运行其方法/函数之一,然后执行以下操作:
查看方法值的一种非常常见的方法是选择类的方法名称并执行“添加监视”,以便您可以在“监视”窗口中看到其当前值。 但是,该对象需要再次实例化并处于要显示的有效值的范围内。 与使用立即窗口相比,它的功能要弱得多,限制也更大。
除了检查方法之外,您还可以做简单的数学方程:
或比较值:
如果您直接在“立即窗口”中,则不需要问号(“?”),但为了清楚起见,将其包含在此处(以区分键入的表达式)与结果。)但是,如果您在命令行窗口中并且需要在立即窗口中执行一些快速操作,则在语句前面加上“?” 然后你就走吧。
Intellisense 在立即窗口中工作,但有时可能有点不一致。 根据我的经验,它似乎只在调试模式下可用,而在设计、非调试模式下则不可用。
不幸的是,立即窗口的另一个缺点是它不支持循环。
One nice feature of the Immediate Window in Visual Studio is its ability to evaluate the return value of a method particularly if it is called by your client code but it is not part of a variable assignment. In Debug mode, as mentioned, you can interact with variables and execute expressions in memory which plays an important role in being able to do this.
For example, if you had a static method that returns the sum of two numbers such as:
Then in the Immediate Window you can type the following:
As you can seen, this works really well for static methods. However, if the method is non-static then you need to interact with a reference to the object the method belongs to.
For example, let’s say this is what your class looks like:
If the object already exists in memory and it’s in scope, then you can call it in the Immediate Window as long as it has been instantiated before your current breakpoint (or, at least, before wherever the code is paused in debug mode):
In addition, if you want to interact and test the method directly without relying on an existing instance in memory, then you can instantiate your own instance in the Immediate Window:
You can take it a step further and temporarily assign the method's results to variables if you want to do further evaluations, calculations, etc.:
Furthermore, if you don’t even want to declare a variable name for a new object and just want to run one of its methods/functions then do this:
A very common way to see the value of a method is to select the method name of a class and do a ‘Add Watch’ so that you can see its current value in the Watch window. However, once again, the object needs to be instantiated and in scope for a valid value to be displayed. This is much less powerful and more restrictive than using the Immediate Window.
Along with inspecting methods, you can do simple math equations:
or compare values:
The question mark ('?') is unnecessary if you are in directly in the Immediate Window but it is included here for clarity (to distinguish between the typed in expressions versus the results.) However, if you are in the Command Window and need to do some quick stuff in the Immediate Window then precede your statements with '?' and off you go.
Intellisense works in the Immediate Window, but it sometimes can be a bit inconsistent. In my experience, it seems to be only available in Debug mode, but not in design, non-debug mode.
Unfortunately, another drawback of the Immediate Window is that it does not support loops.
使用立即窗口执行命令
立即窗口也可用于执行命令。 只需键入
>
后跟命令即可。例如
>shell cmd
将启动一个命令 shell(例如,这对于检查传递给 Visual Studio 的环境变量很有用)。>cls
将清除屏幕。以下是常用命令的列表,它们有自己的别名: https ://msdn.microsoft.com/en-us/library/c3a0kd3x.aspx
Use the Immediate Window to Execute Commands
The Immediate Window can also be used to execute commands. Just type a
>
followed by the command.For example
>shell cmd
will start a command shell (this can be useful to check what environment variables were passed to Visual Studio, for example).>cls
will clear the screen.Here is a list of commands that are so commonly used that they have their own aliases: https://msdn.microsoft.com/en-us/library/c3a0kd3x.aspx
立即窗口用于调试和计算表达式、执行语句、打印变量值等。 它允许您在调试期间输入要由开发语言求值或执行的表达式。
要显示立即窗口,请选择“调试”>“窗口”>“立即”或按 Ctrl-Alt-I
以下是立即窗口的示例:
添加断点
调用命令
< /a>
https://msdn.microsoft.com/en-us/library/ f177hahy.aspx
The Immediate window is used to debug and evaluate expressions, execute statements, print variable values, and so forth. It allows you to enter expressions to be evaluated or executed by the development language during debugging.
To display Immediate Window, choose Debug >Windows >Immediate or press Ctrl-Alt-I
Here is an example with Immediate Window:
add breakpoint
call commands
https://msdn.microsoft.com/en-us/library/f177hahy.aspx