views:

105

answers:

5

Hey Guys

Again, learner here.

Generally I like to be able to look at the whole state of the application to understand what information I have available to me in the current scope.

Something I do a lot is open up QuickWatch and evaluate this which gives me everything inside the current class.

I was wondering if there are any other keywords I can use to get a similar sort of thing which would allow me to interrogate other parts of the application's state. Like, is there any way to see the values inside another class? Is there such a thing as a "global" scope?

Hope you can help and hope I haven't been too vague as I'm still learning the correct terminology to be using in a C# environment.

Thanks in advance for your help :)

Cheers

Iain

+6  A: 

Something that I find really useful is the debugger attribute.

[DebuggerDisplay("X = {this.X}, Y= {this.Y},")]
public class Point
{
   Public Double X { get; set; }
   Public Double Y { get; set; }
}

When you come across this class in the debugger, rather than display the ToString() version of the class, it'll display the string configured in this attribute. Very useful for quick analysis of a class and its members.

EDIT: In fact if you check out the System.Diagnostics namespace you'll find another host of features you might find useful like preventing stepping into trivial methods.

Ian
Hey, that's cool, I will definately use that. Thanks Ian. Nice name btw :)
Iain Fraser
+6  A: 

Why not just use the Autos windows? It does a pretty good job of selecting the most appropriate variable values in any given context.

Another useful windows is the Locals window which gives you more than creating a QuickWatch on this (bug it includes this).

Mark Seemann
Hey that's nice. That's another thing I had no idea about, I'll use this for sure. I'm going to leave the question open for a bit to see if there are any other ideas :)
Iain Fraser
+1  A: 

I find the immediate window very usefull.

When your application iswaiting on a breakpoint you can write an execute code from this window.

Henrik Jepsen
+1  A: 

I find the Immediate window useful too. (Debug menu -> Windows -> Immediate)

I like to use the "?" shortcut to print the result e.g.

? 0xffff

to get the decimal value of a hex number, or

? String.Format("{0:G2}", 1.23456f)

to see a formated number.

Up and down arrow keys let you navigate through commands you've typed previously, and edit them - very useful for quick trial and error experimentation.

Note that you don't need to be stopped at a breakpoint to use this.

Tom Bushell
+1  A: 

Another useful technique is to uses System.Diagnostics to print diagnostic and status information to the Output window (Debug menu -> Windows -> Output).

This is often a more convenient alternative than printing to a separate console window,

using System.Diagnostics;
...
Debug.WriteLine("This is a diagnostic message");
Tom Bushell