How do you use the Immediate Window in Visual Studio?
Asked Answered
K

3

142

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?

Klemperer answered 27/4, 2009 at 16:25 Comment(3)
If you referring to issue individual Visual Studio commands then have a look at [Immediate Window][1] for examples. That show how to evaluate expressions, execute statements, print variable values, and so forth though the immediate window. [1]:msdn.microsoft.com/en-us/library/f177hahy(VS.80).aspxBurdett
There are some special commands that are hard to find a list of. msdn.microsoft.com/en-us/library/ms171362(v=vs.100).aspx Some of the power of windbg is available. Used to be able, given an address, find the closest symbol. (ln) I think that got stupidly turned off. I'd love to have windbg's dt command to display the types in structs, etc. At least you can type an object variable to get all the members in a list with their values. (rather than trying to play the open a node close a node game.)Stealth
for Visual Studio 2017, the link to relevant documentation is learn.microsoft.com/en-us/visualstudio/ide/reference/…Vanna
T
149

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.

Trafficator answered 1/9, 2009 at 7:57 Comment(2)
How do you find the immediate window?Aqaba
Ctrl + Alt + I or Debug -> Windows -> ImmediateTrafficator
C
15

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

Confiteor answered 27/2, 2017 at 18:57 Comment(1)
nice tip. In fact, this combines nicely with another trick I use: >open Filename will open the selected file in the solution, and even has filename completion.Klemperer
P
11

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

Prisilla answered 22/11, 2017 at 7:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.