tags:

views:

492

answers:

4

I´m coming from a PHP background where my debugging "tools" are basically echo, var_dump and exit. While I know the importance of debugging, I never tried to use/learn a debugging tool.

Now I´m learning C# and I think I can´t really program without an extensive knowledge of this area.

So my question is: where can I learn what is and how to do debugging? I know what is a breakpoint (conceptually), but how to use it? How to use Step into and Step over? Basic things like that.

As a related question, there is anything like var_dump in C# (Visual Studio), where I can inspect any object.

I find very difficult and painful to do a foreach for every array/list to see the contents, specially now that I´m still learning the language.

+11  A: 

Microsoft has an extensive guide on C# debugging in Visual Studio that might be helpful.
VS has a lot of powerful debugging functionality; for example, rather than doing a foreach to see the contents of an array as you were describing, you could set a breakpoint (pausing the execution of the program) and select the variable you wish to see the contents of (array or list or whatever) and see what it contains, without having to write any extra code.
Step Into and Step Over can be used to continue execution of the program but only incrementally so that you can continue to see how variables change, where the flow of execution currently is, etc.

Donut
Thank you for the links. The first one from MSDN is very complete.
Luiz Damim
+4  A: 

This has been covered earlier on StackOverflow:

http://stackoverflow.com/questions/788807/best-visual-studio-2008-debugging-tutorial

clintp
Thank you. I searched SO but didn´t find this question.
Luiz Damim
For learning how to do something, a good google/SO keyword is "tutorial".
clintp
+2  A: 

So, your compiled applications can be run in a "debug" mode from which visual studio can monitor the internal workings of the application and even control it.

A break point can be placed just about anywhere in your code by clicking to the far left of the line (kinda in the margin of the visual studio text editor). When that line of code has been reached, the visual studio debugger will actually pause the execution of your program and bring you back to the editor where you can literally hover over a variable or object or whatever and see everything about it.

There is also a "Locals" window available that will give you the break down of all of your locally scoped items - this should pop up by default at the bottom of your screen when debugging.

In debug mode you can navigate the execution of your code line-by-line

F10 will continue with the next line of code.

F11 will attempt to drill down into what ever functions are on the current line of code

Ctrl-D will bring up a "Quick Watch" window giving you all information about the currently selected variable/object.

Once you are in debug mode there are tons of things you can do - in some cases you can even edit the code as you go.

The easiest way to get into debug mode is to use the little "play" button up at the top of visual studio - and when a break point is reached it will enter debug mode and highlight the currently executing line of code.

You can also hit F10 from the editor and your application will be started and paused on the very first line of code.

By comparison, in PHP, you had to actually write "debugging code" into your application - using Visual Studio you can actually monitor the execution of your code without adding a thing to your existing code.

I hope that gets you started.

You might want to also read up on your IDE a bit to. There is a metric ton of stuff in visual studio that will help you navigate your code in ways you never imagined in most PHP editors.

DataDink
A: 

If you've already downloaded Microsoft Visual Studio, you'd might want to check out the Visual C# Express Library available for free over at: http://msdn.com/express/

It's located down the bottom of the page and is very useful. It contains pretty much every answer you might be looking for as a beginner to the C# Language. ...Welcome to C#, my friend :-D

baeltazor