Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Hands-On Visual Studio 2022 - Second Edition

You're reading from  Hands-On Visual Studio 2022 - Second Edition

Product type Book
Published in Mar 2024
Publisher Packt
ISBN-13 9781835080443
Pages 336 pages
Edition 2nd Edition
Languages
Authors (2):
Hector Uriel Perez Rojas Hector Uriel Perez Rojas
Profile icon Hector Uriel Perez Rojas
Miguel Angel Teheran Garcia Miguel Angel Teheran Garcia
Profile icon Miguel Angel Teheran Garcia
View More author details

Table of Contents (19) Chapters

Preface 1. Part 1: Visual Studio Overview
2. Chapter 1: Getting Started with Visual Studio 2022 3. Chapter 2: Creating Projects and Templates 4. Chapter 3: Debugging and Profiling Your Apps 5. Part 2: Tools and Productivity
6. Chapter 4: Adding Code Snippets 7. Chapter 5: Coding Efficiently with AI and Code Views 8. Chapter 6: Using Tools for Frontend and Backend Development 9. Chapter 7: Styling and Cleanup Tools 10. Chapter 8: Publishing Projects 11. Part 3: GitHub Integration and Extensions
12. Chapter 9: Implementing Git Integration 13. Chapter 10: Sharing Code with Live Share 14. Chapter 11: Working with Extensions in Visual Studio 15. Chapter 12: Using Popular Extensions 16. Chapter 13: Learning Keyboard Shortcuts 17. Index 18. Other Books You May Enjoy

Debugging and Profiling Your Apps

As software developers, one skill that should be learned as early as possible is program debugging. This applies to .NET projects as well as to any other software development technology.

If you want to get the most out of VS 2022, you must be familiar with its different windows, which can help you observe information so that you can fix bugs and know how to use as many of the debugging tools it offers, including breakpoints. A breakpoint offers the functionality to stop the execution of an application, allowing you to see the state of each of the objects and corroborate its behavior.

Another set of tools that will help you on your way as a professional developer is profiling tools, which help with finding bottlenecks that can cause poor application performance.

That is why, in this chapter, we will talk about the different tools and options for debugging and profiling applications in VS 2022.

The topics we will discuss in this chapter are...

Technical requirements

To follow the examples presented in this chapter, VS 2022 must be installed with any workload, as described in Chapter 1, Getting Started with Visual Studio 2022.

You can find the practice project for this chapter at https://github.com/PacktPublishing/Hands-On-Visual-Studio-2022-Second-Edition/tree/main/Chapter%203.

Important note

VS has keyboard shortcuts for almost all the operations we are going to be performing. So, I will mention them for you as Shortcut notes as we continue with this chapter.

Understanding compilation in VS 2022

VS is an excellent IDE that makes the process of compiling applications transparent to developers. For example, after creating a project, it is enough to press a button to run any application. However, understanding some details of the VS compilation process can help you optimize code, diagnose errors, and take full advantage of the IDE.

In this section, I am not going to go into the details of concepts such as what a compiler is or the process of converting high-level code into machine language; instead, I am going to give you some tips and advice that you can take advantage of.

Let’s look at how we can compile applications in VS.

How to build a project in VS 2022

Once you create a project in VS using any of the templates we discussed in Chapter 2, Creating Projects and Templates, one of the first things you should do is compile the project. This is because, during the steps of compilation, VS converts the high-level code into...

Debugging projects in VS

Before discussing breakpoints in depth in the Exploring breakpoints in VS 2022 section, you must know about some technical aspects that are used in the debugging world, as well as in VS.

We’ll start with what debugging and debugger are.

Understanding the technical aspects of debugging

You must know the difference between the terms debugger and debugging so that you know what I mean when I mention any of these terms throughout this book.

First, the term debugging refers to the action of looking for errors in the code. This does not necessarily include the use of a tool such as an IDE. You could, for example, search for errors in code written on a piece of paper, and you would still be debugging.

This is usually not feasible, and a tool called a debugger is often used. This tool is attached to the application process you are going to run, allowing you to analyze your code while the application is running.

In VS, we can start an application...

Exploring breakpoints in VS

Breakpoints are a fundamental part of software development. They allow you to stop the flow of your application at any point where you want to inspect the state of your objects.

To place a breakpoint in VS, it is enough to position ourselves right next to the numbering of the lines. A gray circle will appear and disappear as we move our cursor over the line numbers.

Once we find the line we want to debug, we just need to left-click once, which will cause the circle to turn red, as shown in Figure 3.11. Once you have done this, if you move the cursor away from the circle, you will be able to see that it remains as-is, as shown in the following screenshot:

Figure 3.11 – Placing a breakpoint

Figure 3.11 – Placing a breakpoint

If we proceed to execute the application with the breakpoint set, we will see how the application flow stops immediately after starting the application, as shown in Figure 3.12:

Figure 3.12 – Debugging a breakpoint

Figure 3.12 – Debugging...

Inspection tools for debugging

When working with breakpoints, we must know where to find the information we want to visualize and whether that information is correct or not. For this, within VS, we have a series of windows that will allow us to visualize different types of information. So, let’s take a look at the most important ones.

The Watch window

The Watch window allows us to keep track of the values of variables or properties while we execute our code step by step. This window is especially useful when we have pieces of code that are repeated several times, such as cycles or common methods. To access this window, we must place a breakpoint in the code and execute the application.

Once the application stops at the breakpoint, we will be able to deploy the Debug | Windows menu. This will show us a set of new debugging windows that we can only access while running the application. Let’s select the Watch option so that we can choose a window, as shown in Figure...

Measuring app performance with profiling tools

Application bottlenecks are a problem you will encounter during your career as a developer on any platform. It is for this reason that you must learn to manage the IDE tools that allow you to analyze the performance of your applications and objects created during the life cycle of your application. In this section, you will learn about these tools through a case study.

Analyzing the performance of an application

The first tool we will talk about is the CPU Usage diagnostic tool, which allows us to measure CPU usage during the execution of an application. This tool works thanks to the different breakpoints and debugger stops. Let’s see this practically.

In the project you downloaded from GitHub, in the Program.cs file, you will find the following code section:

var performanceDemo = new PerformanceDemo();performanceDemo.Run();
Console.WriteLine("Press any key to exit...");

This example simulates the invocation...

Summary

VS has many options for debugging source code. In this chapter, we learned what breakpoints are, the different types of breakpoints, and the associated windows that we can activate to keep track of data in variables and properties. This information is of utmost importance since it will help you solve bugs in your code when you face them.

Likewise, you learned how to measure the performance of your applications, both in terms of CPU usage to detect functions that have the highest workload, as well as to detect places in the code that may be creating object references that are not released.

In the next chapter, you will learn about the concept of code snippets, which allow you to reuse common pieces of code across different projects and quickly adjust them to suit your needs.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Hands-On Visual Studio 2022 - Second Edition
Published in: Mar 2024 Publisher: Packt ISBN-13: 9781835080443
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}