你如何在 Visual Studio 中使用即时窗口? [英] How do you use the Immediate Window in Visual Studio?

查看:51
本文介绍了你如何在 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.

您如何使用即时窗口?

推荐答案

Visual Studio 中的即时窗口的一个很好的特性是它能够评估方法的返回值,特别是如果它被客户端代码调用但它是不是变量赋值的一部分.如前所述,在调试模式下,您可以与变量进行交互并在内存中执行表达式,这对于执行此操作起着重要作用.

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"

另外,如果你想直接交互和测试方法而不依赖内存中的现有实例,那么你可以在即时窗口中实例化你自己的实例:

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

如果您直接在即时窗口中,则不需要问号 ('?'),但为了清楚起见,将其包含在此处(以区分键入的表达式与结果.)但是,如果您在命令中窗口,需要在立即窗口中做一些快速的事情,然后在你的语句之前加上?"然后你走.

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 在即时窗口中工作,但有时可能有点不一致.根据我的经验,它似乎只在调试模式下可用,而在设计、非调试模式下不可用.

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 中使用即时窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆