visual-studio 你如何在 Visual Studio 中使用即时窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/794255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:24:40  来源:igfitidea点击:

How do you use the Immediate Window in Visual Studio?

visual-studiodebuggingimmediate-window

提问by Phillip Ngan

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?

如何使用立即窗口?

回答by Ray

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 notpart 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.

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

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 beforeyour 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 owninstance 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

or compare values:

或比较值:

? 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 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.

Intellisense 在 Immediate Window 中工作,但有时可能有点不一致。根据我的经验,它似乎只在调试模式下可用,而在设计、非调试模式下不可用。

Unfortunately, another drawback of the Immediate Window is that it does not support loops.

不幸的是,立即窗口的另一个缺点是它不支持循环。

回答by CJBS

Use the Immediate Window to Execute Commands

使用立即窗口执行命令

The Immediate Window can also be used to execute commands. Just type a >followed by the command.

即时窗口也可用于执行命令。只需键入 a>后跟命令。

enter image description here

在此处输入图片说明

For example >shell cmdwill start a command shell (this can be useful to check what environment variables were passed to Visual Studio, for example). >clswill clear the screen.

例如,>shell cmd将启动一个命令外壳程序(例如,这对于检查传递给 Visual Studio 的环境变量很有用)。>cls将清除屏幕。

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

下面是一个非常常用的命令列表,它们有自己的别名:https: //msdn.microsoft.com/en-us/library/c3a0kd3x.aspx

回答by Isma Rekathakusuma

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

要显示立即窗口,请选择 Debug >Windows >Immediate 或按 Ctrl-Alt-I

enter image description here

在此处输入图片说明

Here is an example with Immediate Window:

这是立即窗口的示例:

int Sum(int x, int y) { return (x + y);}
void main(){
int a, b, c;
a = 5;
b = 7;
c = Sum(a, b);
char temp = getchar();}

add breakpoint

添加断点

enter image description here

在此处输入图片说明

call commands

调用命令

enter image description here

在此处输入图片说明

https://msdn.microsoft.com/en-us/library/f177hahy.aspx

https://msdn.microsoft.com/en-us/library/f177hahy.aspx