如何使用 Visual Studio 中的立即窗口?

发布于 2024-07-17 23:39:41 字数 99 浏览 6 评论 0原文

立即窗口是一个非常有用的调试应用程序的工具。 它可用于执行在断点上下文中有效的代码语句并检查值。 我还用它来输入代码片段来学习语言功能。

如何使用立即窗口?

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 技术交流群。

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

发布评论

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

评论(3

怪我鬧 2024-07-24 23:39:41

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

例如,如果您有一个返回两个数字之和的静态方法,例如:

private static int GetSum(int a, int b)
{
    return a + b;
}

然后在立即窗口中您可以键入以下内容:

? GetSum(2, 4)
6

如您所见,这对于静态方法非常有效。 但是,如果该方法是非静态的,那么您需要与该方法所属的对象的引用进行交互。

例如,假设您的类如下所示:

private class Foo
{
    public string GetMessage()
    {
        return "hello";
    }
}

如果该对象已存在于内存中并且在作用域内,那么只要它在您的调用之前被实例化,您就可以在立即窗口中调用它。当前断点(或者至少在调试模式下代码暂停的任何地方之前):

? foo.GetMessage(); // object ‘foo’ already exists
"hello"

此外,如果您想直接交互和测试方法而不依赖于内存中的现有实例,那么您可以实例化您自己的断点 立即窗口中的实例:

? Foo foo = new Foo(); // new instance of ‘Foo’
{temp.Program.Foo}
? foo.GetMessage()
"hello"

如果您想进行进一步的评估、计算等,您可以更进一步,暂时将方法的结果分配给变量:

? string msg = foo.GetMessage();
"hello"
? msg + " there!"
"hello there!"

此外,如果您甚至不想声明一个新对象的变量名称,并且只想运行其方法/函数之一,然后执行以下操作:

? new Foo().GetMessage()
"hello" 

查看方法值的一种非常常见的方法是选择类的方法名称并执行“添加监视”,以便您可以在“监视”窗口中看到其当前值。 但是,该对象需要再次实例化并处于要显示的有效值的范围内。 与使用立即窗口相比,它的功能要弱得多,限制也更大。

除了检查方法之外,您还可以做简单的数学方程:

? 5 * 6
30

或比较值:

? 5==6
false
? 6==6
true

如果您直接在“立即窗口”中,则不需要问号(“?”),但为了清楚起见,将其包含在此处(以区分键入的表达式)与结果。)但是,如果您在命令行窗口中并且需要在立即窗口中执行一些快速操作,则在语句前面加上“?” 然后你就走吧。

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:

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

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.

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

揪着可爱 2024-07-24 23:39:41

使用立即窗口执行命令

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

输入图像描述这里

例如 >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.

enter image description here

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

并安 2024-07-24 23:39:41

立即窗口用于调试和计算表达式、执行语句、打印变量值等。 它允许您在调试期间输入要由开发语言求值或执行的表达式。

要显示立即窗口,请选择“调试”>“窗口”>“立即”或按 Ctrl-Alt-I

在此处输入图像描述

以下是立即窗口的示例:

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();}

添加断点

在此处输入图像描述

调用命令

在此处输入图像描述< /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

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

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