I am working in Microsoft Visual C# 2008 on a Windows Form application.
I would like to write some variables out to a window in the IDE to determine what values they contain. I thou开发者_StackOverflow社区ght perhaps I could write to the console using console.writeline however I did not see where I could open a console window.
Is there a command I should be using to write to the immediate window or some other place where the information can easily be seen in the IDE?
Use Debug.WriteLine(). It's output goes to the Output window. Console.WriteLine() works the same way in a Winforms app but using Debug is better since that code automatically gets removed in the Release build.
And of course, you'll want to leverage the debugger first.
You can use Trace also, which has more features: http://msdn.microsoft.com/en-us/library/4y5y10s7.aspx
Trace.WriteLine("Error ")
If you just want to see what the current value of a variable is, put a breakpoint in your code somewhere where this variable is referenced (a breakpoint is a red dot that will appear if you click on the left side of your code window).
Then just run the program, and when your breakpoint is hit the execution will suspend right on the breakpoint. Just hold your mouse over the variable and the popup will show you what the value is.
You can use the Spywindows in debug mode.
Console.WriteLine() writes to the standard output stream. while Debug.WriteLine() writes to all trace listeners in the Listeners collection, it is possible that this could be output in more than one place (VS output window, Console, Log file)
1) Use Debug.WriteLine to output to the debug window in DEBUG builds.
2) Use Console.WriteLine when you need to output to a console (unless it's a console application it also outputs to the debug window)
3) Use Trace.WriteLine to output to the output window in all builds. It's output can also be seen when running Mark Russinovich's (formerly SysInternals) Dgbview, which allows you to look at trace statements in a running process (without any debugger attached).
4) Use a trace-point: put a break-point on the line of interest, right-click the red bullet indicating the break-point, choose "When hit..", check "Print A message" and in the window type something like "The value of x is {x}" where x is your variable. The expression in the curly braces will be evaluated for output. This can be useful when you don't want to edit your code.
精彩评论