Last week I talked about how useful the Immediate Window is during debug-mode. A less-known, but totally awesome, feature of the Immediate Window is that it can be used at design-time to test/debug your code. This is particularly useful if you are working on a particular class or method that you want to test without running the entire application.
The best way to understand this is to see it in action. The screenshot below shows a new Customer object being created in a C# project within the Immediate window.

When you declare a new object in the Immediate window at design-time, Visual Studio will create an instance of that object. However even though it has created this object, Visual Studio will remain in design-mode (For those that are interested, it actually uses the Object Test Bench in the background). If you invoke a method or property that contains an active breakpoint, Visual Studio will change to debug mode and break at the breakpoint. Once you resume execution from the break Visual Studio will return to design-mode.
There are a few gotcha’s and caveats that you need to be aware of when performing design-time expression evaluation.
Firstly, within a Visual Basic project you can’t use an explicit variable declaration (for example, Dim myCust as Customer). Instead variables are declared implicitly using the assignment operator (for example, myCust = new Customer()).
Secondly, if you are using C#, IntelliSense is not available in the Immediate window during design-time expression evaluation. Visual Basic programmers are a little more spoilt, as they are given a limited form of IntelliSense here.
Thirdly, any statements you enter into the Immediate Window will be evaluated against whatever project is selected in the Solution Explorer. If no project is selected,Visual Studio will evaluate it against the startup project. If the code doesn’t exist is that project, you will get an error.
Finally, you can’t use design-time expression evaluation in any project that requires an execution environment. This includes Web projects, VSTO projects, Smart Device projects, and SQL projects. However as I mentioned earlier, the scope of this is per-project. Therefore, if you have a solution that contains a web project and a separate class library project for your business logic, you can use this method to test any of the business logic code.


[...] Dave Gardner’s Debugging Tip of the Week #2 covers Design-time Expression Evaluation. [...]