Techniques

This outline covers debugging techniques like stepping through code, setting breakpoints, and inspecting variables to understand the code’s execution in the context of the project found at https://github.com/stevedunn/bindingtodefaultablelist.

Stepping Through Code

Stepping through code is a technique that allows you to execute a program one line at a time. This can be useful for understanding how the code works and identifying potential problems.

Steps to Step Through Code:

  1. Set a Breakpoint: Place a breakpoint at the line of code you want to start stepping through.
  2. Run the Program: Start the program in debug mode.
  3. Step Over: Execute the current line of code and move to the next.
  4. Step Into: If the current line of code calls a function, step into the function to execute it line by line.
  5. Step Out: If you are inside a function, step out to return to the calling function.

Example:

To step through the code in the DefaultList class, place a breakpoint at the beginning of the add method and then run the program in debug mode.

Setting Breakpoints

Breakpoints are markers that tell the debugger to pause program execution at a specific line of code. This allows you to inspect the state of the program at that point.

Types of Breakpoints:

  • Line Breakpoints: These are the most common type of breakpoint. They pause execution at a specific line of code.
  • Conditional Breakpoints: These breakpoints are only triggered when a certain condition is met. This can be useful for debugging specific scenarios.
  • Exception Breakpoints: These breakpoints are triggered when a specific exception is thrown. This can be helpful for debugging errors.

Example:

To set a breakpoint at the line where the add method is called in the DefaultList class, click on the left gutter next to the line number.

Inspecting Variables

Inspecting variables allows you to see the current value of a variable at a specific point in time. This can be useful for understanding how the code is working and identifying potential problems.

How to Inspect Variables:

  • Watch Window: You can add variables to a watch window to monitor their values as the program executes.
  • Debug Window: The debug window shows the values of variables in the current scope.

Example:

To inspect the value of the items variable in the DefaultList class, add it to the watch window and then step through the code.

Logging

Logging can be used to write information about the program’s execution to a file or console. This can be helpful for tracking down problems and understanding how the code is working.

Log Levels:

  • Debug: Used for detailed information about the program’s execution.
  • Info: Used for general information about the program’s execution.
  • Warn: Used for warnings about potential problems.
  • Error: Used for error messages.

Example:

To log information about the add method in the DefaultList class, you can use the following code:

// DefaultList.cs
          using System;
          using System.Collections.Generic;
          using System.Linq;
          
          namespace BindingToDefaultableList
          {
              public class DefaultList<T> : IList<T>
              {
                  private List<T> _items = new List<T>();
          
                  // ...
          
                  public void Add(T item)
                  {
                      // Log the item being added
                      System.Diagnostics.Debug.WriteLine($"Adding item: {item}");
          
                      _items.Add(item);
                  }
          
                  // ...
              }
          }