Debugging Tips for C# on Visual Studio

Houman Jafarnia
Full-Stack Developer

Developers, meet maximum productivity. Whether you’re looking to avoid code smells, complete general debugging, or step through compiled code, this guide will streamline your process in Visual Studio.

Using ReSharper

Code Analysis and Fixes

By default, Visual Studio only shows errors for the currently open file. However, you can use ReSharper’s solution-wide analysis to show any issues in your solution. This way, ReSharper shows you all your errors and warnings in one spot. Be mindful that this function can be resource-intensive.

Now, you have a bird’s eye view of your entire solution. This can highlight different parts of code that aren’t going to necessarily break your code if they aren’t addressed, but are certainly good to investigate, especially if you like to be thorough like me.

Additionally, you can also configure the nature of the flags in ReSharper. To configure them, simply

  1. Open the ReSharper app
  2. Click on Manage Options
  3. Click on Inspection Severity
  4. Select the options you want to be triggered
  5. Configure whether you want them to show up as a warning, suggestion, hint, or error

Navigation and Shortcuts

Shortcuts are designed to simplify our lives, but ReSharper takes shortcutting one step further. ReSharper uses shortcuts to automatically generate code (like classes or constructors). While some shortcuts are built into ReSharper, you can also customize the app by adding more. These shortcuts also assist with reorganizing code efficiently.

For example, when I enter the shortcut “ctor” and press enter, ReSharper will automatically generate a constructor for me:

Want to work with a new library in conjunction with your solution? If your library hasn’t been imported yet, ReSharper will search NuGet for similar libraries based on the name you enter. This will also automatically import the library into your project and add any necessary using statements.

Refactoring Code and Cleanup

You might be using several references to an object within your code. So, you can imagine that if another area of the solution is accessing an object for a specific variable, if you change it within one object, you’re likely going to want to change it everywhere else too. ReSharper’s refactoring tool features the ability to analyze the entire code while giving you the option to rename all the references within the solution. This helps a lot with refactoring code and makes project clean-up substantially easier and faster.

General Debugging and Troubleshooting

Tracking Local, Auto, and Watch Variables

To assist with general debugging, Visual Studio allows you to easily track local, auto, and watch variables.

  • Autos tab: Shows you variables that relate to the current function you’re running
  • Locals tab: Shows you all the variables that are locally related to the current function of everything that’s been initialized
  • Watch tab: Allows you to type in any variable that you want to access. You can also get static variables from classes that are outside the scope of the current class you’re running through.

You can also edit objects within the variables themselves while your code is running. You just need to be careful when trying to assign a new object as you need to use the new keyword.

Step In, Step Over, Step Out, and Moving Execution

While most IDEs only allow you to step in, step over, and step out, Visual Studio also gives users the ability to step backwards. Stepping backwards provides you with a historical record showing you what the variables were in a previous line of code (also known as “historical debugging”). Visual studio can also allow you to move your current line of execution while debugging. Pair this with Live Edits and you can edit code, move the execution back, and resume execution to test your code.

Live Editing of Code

The modern framework of Visual Studio allows you to edit your inline code while it’s running, which is especially handy during the debugging process. This doesn’t work with every framework, but most modern frameworks should allow you to do this. In newer versions of Visual Studio, they’ve further expanded it to a new “Hot Reload” feature which lets you edit UI, decide when you want the changes to be pushed through, and even push the changes while the application is running.

Disassembly and Stepping Through Compiled Code

Setup of Visual Studio Settings

Let’s say you’re working on a project consisting of a third-party library, which could possibly introduce an error into your code. By default, Visual Studio has the option “Just My Code” enabled. By disabling this, we can step into external libraries to better understand what’s going on.

  1. By going into: Tools > Options
  2. Select Debugging > General
  3. Uncheck Enable Just My Code (enabled by default)
  4. Visual Studio and ReSharper can then give you several options to step into the code. They can both either try to decompile the code for you, or ReSharper can try retrieving the source code from the library (assuming it’s an open source).
  5. Now, you can step into the third-party code to continue debugging.

Decompilation and Stepping Through

If the debugging symbols for your libraries haven’t been loaded, you can load symbols with Visual Studio or ReSharper Decompiler. That way you can get a rough idea of the code, which is useful for third-party or client libraries. This is very handy for understanding any exceptions generated by external libraries.

  1. First, make sure that you are debugging a running project.
  2. On the top toolbar, click Debug > Windows > Modules
  3. The Modules window lists all the various libraries that have been loaded, where they have been loaded from, and whether their debugging symbols have been loaded.
  4. You can then right-click on that library and Load Symbols Using ReSharper Decompiler.
Through this module window, you can also load symbol files that could be generated from third-party tools.

Partial Recompilation and Implications

If you have a library of a specific application, you can also inject code into that existing DLL using third-party tools. This is relatively easy in C# and Java, too, due to the designs of each framework (they’re run under another layer before they’re processed as machine code, making them easier to reverse-engineer).

Conclusion

Whatever the goals of your project, the tools built into ReSharper make your life a whole lot easier when coding in C#. Experiment with the tools to discover what works best for you in boosting your productivity.


Join Our Team

Indellient is a Software Development Company that specializes in Data AnalyticsCloud Services, and DevOps Services.

We’re dedicated to creating an encouraging, inclusive, and fruitful work environment for all of our team members. Check out open opportunities on our Careers page.


About The Author

Houman Jafarnia

I’m a full-stack developer at Indellient. I’ve worked with computers since I was as little as 4 years old. I try to code in my spare time whenever I can and I’m passionate about technology. I love digging into code and reverse engineering is one of my favorite things to do with programming. Without these tools, I’m afraid I’d never be able to scratch that itch.