Bogdan
Bogdan

Reputation: 63

Debugging line by line in c#

I found on microsoft site that if I want to debug a program line by line I need to press F11. If I press F11 I obtain (in a new form named Program.cs):

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Windows.Forms;

     namespace WindowsFormsApplication1
    {
      static class Program
     {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();  
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
    }
    }

They add that "When stepping through code, the line that is about to be executed is highlighted". In my case is highlighted:

       Application.EnableVisualStyles();

Should I choose another line?

Then, they tell to choose Stop Debugging to make the code editor writable.

If I follow this steps, I don't see any modifications. Should I see a new form in which the computer debug line by line? Should I do another thing?

I'm sorry if my question seems stupid. Please understand me that I am a begginer to C#.

Thank you so much!

Upvotes: 0

Views: 3854

Answers (3)

vguzmanp
vguzmanp

Reputation: 825

I think you need a few tips on how debugging works:

First of all, you should insert a simple breakpoint (F9 in Visual Studio) somewhere in the code you want to debug. Let's say you have a Windows Form Application, with a Form called Form1. In this form you have a Button which, when clicked, does something. You would have something like this:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        doSomething(...);
    }
}

To stop the execution of the program when you click the button (so you can see exactly what's happening), you can put a breakpoint on the click event. Once you have the breakpoint there, every time the click event is fired, the program will stop, and you will be able to run it step-by-step.

By running it step-by-step, you can see the status of your variables and the data you have stored. So you can locate where your code starts to behave differently from what you expected (what is usually called a bug).

To do so, you can step through the program procedure by procedure (F10) or instruction by instruction (F11). F10 will jump over function calls, executing them until they finish. F11 will enter into function calls, executing step by step every instruction in them.

Upvotes: 1

Tigran
Tigran

Reputation: 62248

Your question has less relation to concrete language (C# in the tag) and much more about Visual Studio environment.

You press F11 to execute not line by line, but instruction-by-instruciton, cause if you immagin the code like this:

if(condition) { x++; DoSomething(); ...}, on press of F11 it will not jump all this line but will execute

  • first if
  • after x++
  • after DoSomething(..)

and all this is on the same line in your code, but for compiler these are different instructions.

If you want to make editor Editable, it's enough to press Pause button. But by clicking Pause button, in case if in the scope of the current function there is no any lambda expression, you will be able to change the code just in Pause, they call this feature Edit and Continue

Hope this helps.

Upvotes: 2

pwnercl
pwnercl

Reputation: 1

You should mark a debug point (or breakpoints), and then start debugging your program. Is not recommended, in my opinion, to start debugging line by line your program, you should debug just the parts that throws exceptions, and for that you mark breakpoints on your program (How to: http://msdn.microsoft.com/en-us/library/k80ex6de.aspx). When your program reachs that line, the debugger starts an detail debug line by line if you want.

Upvotes: 0

Related Questions