问题描述
即时窗口是调试应用程序的非常有用的工具.它可用于执行在断点上下文中有效的代码语句并检查值.我还使用它来键入代码段以学习语言功能.
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.
您如何使用即时窗口?
推荐答案
Visual Studio中即时窗口的一个不错的功能是它能够评估方法的返回值,特别是如果它是由客户端代码调用的,但是变量赋值的 not 部分.如前所述,在调试"模式下,您可以与变量交互并在内存中执行表达式,这对于执行此操作起着重要作用.
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:
private static int GetSum(int a, int b)
{
return a + b;
}
然后在立即窗口中输入以下内容:
Then in the Immediate Window you can type the following:
? GetSum(2, 4)
6
如您所见,这对于静态方法非常有效.但是,如果该方法是非静态的,则需要与对该方法所属的对象的引用进行交互.
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:
private class Foo
{
public string GetMessage()
{
return "hello";
}
}
如果对象已经存在于内存中并且在范围内,那么只要在当前断点之前(或者至少在任何地方之前)将其实例化,就可以在即时窗口中调用该对象代码在调试模式下暂停):
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):
? foo.GetMessage(); // object ‘foo’ already exists
"hello"
此外,如果要直接交互并测试该方法而不依赖于内存中的现有实例,则可以在即时窗口"中实例化您的 own 实例:
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:
? Foo foo = new Foo(); // new instance of ‘Foo’
{temp.Program.Foo}
? foo.GetMessage()
"hello"
如果要进行进一步的评估,计算等,则可以更进一步,并暂时将方法的结果分配给变量:
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.:
? string msg = foo.GetMessage();
"hello"
? msg + " there!"
"hello there!"
此外,如果您甚至不想为新对象声明变量名,而只想运行其方法/函数之一,请执行以下操作:
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:
? new Foo().GetMessage()
"hello"
查看方法值的一种非常常见的方法是选择类的方法名称并执行添加监视",以便您可以在监视"窗口中查看其当前值.但是,再次需要实例化对象并在范围上显示有效值.与使用立即窗口相比,它的功能要弱得多,限制要大得多.
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:
? 5 * 6
30
或比较值:
? 5==6
false
? 6==6
true
如果您直接位于即时窗口"中,则问号('?')是不必要的,但为了清楚起见,在此处包含了问号(以区分表达式中键入的内容和结果.)但是,如果您在Command中窗口,需要在立即窗口"中做一些快速的操作,然后在语句前加上?"然后离开.
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在即时窗口"中起作用,但有时可能会有些不一致.以我的经验,它似乎仅在Debug模式下可用,而在设计,非调试模式下不可用.
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.
这篇关于如何在Visual Studio中使用立即窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!