
- •Table of Contents
- •About the Author
- •About the Technical Reviewer
- •Acknowledgments
- •Introduction
- •Installing Visual Studio
- •Visual Studio 2022 System Requirements
- •Operating Systems
- •Hardware
- •Supported Languages
- •Additional Notes
- •Visual Studio Is 64-Bit
- •Full .NET 6.0 Support
- •Using Workloads
- •The Solution Explorer
- •Toolbox
- •The Code Editor
- •New Razor Editor
- •What’s Available?
- •Hot Reload
- •Navigating Code
- •Navigate Forward and Backward Commands
- •Navigation Bar
- •Find All References
- •Find Files Faster
- •Reference Highlighting
- •Peek Definition
- •Subword Navigation
- •Features and Productivity Tips
- •Track Active Item in Solution Explorer
- •Hidden Editor Context Menu
- •Open in File Explorer
- •Finding Keyboard Shortcut Mappings
- •Clipboard History
- •Go To Window
- •Navigate to Last Edit Location
- •Multi-caret Editing
- •Sync Namespaces to Match Your Folder Structure
- •Paste JSON As Classes
- •Enable Code Cleanup on Save
- •Add Missing Using on Paste
- •Features in Visual Studio 2022
- •Visual Studio Search
- •Solution Filters
- •Visual Studio IntelliCode
- •Whole Line Completions
- •Visual Studio Live Share
- •Summary
- •Visual Studio Project Types
- •Various Project Templates
- •Console Applications
- •Windows Forms Application
- •Windows Service
- •Web Applications
- •Class Library
- •MAUI
- •Creating a MAUI Application
- •Pairing to Mac for iOS Development
- •Consuming REST Services in MAUI
- •The Complete Weather App
- •The Target Platforms
- •The Required NuGet Package
- •The Weather Models
- •The WeatherService
- •The MainViewModel
- •Registering Dependencies
- •Building the MainPage View
- •Using SQLite in a MAUI Application
- •The ToDoItem Model
- •The ToDoService
- •The MainViewModel
- •Registering Dependencies
- •Building the MainPage View
- •Managing NuGet Packages
- •Using NuGet in Visual Studio
- •Hosting Your Own NuGet Feeds
- •Managing nmp Packages
- •Creating Project Templates
- •Creating and Using Code Snippets
- •Creating Code Snippets
- •Using Bookmarks and Code Shortcuts
- •Bookmarks
- •Code Shortcuts
- •Adding Custom Tokens
- •The Server Explorer
- •Running SQL Queries
- •Visual Studio Windows
- •C# Interactive
- •Code Metrics Results
- •Maintainability Index
- •Cyclomatic Complexity
- •Class Coupling
- •Send Feedback
- •Personalizing Visual Studio
- •Adjust Line Spacing
- •Document Management Customizations
- •The Document Close Button
- •Modify the Dirty Indicator
- •Show Invisible Tabs in Italics in the Tab Drop-Down
- •Colorize Document Tabs
- •Tab Placement
- •Visual Studio Themes
- •Summary
- •Setting a Breakpoint
- •Step into Specific
- •Run to Click
- •Run to Cursor
- •Force Run to Cursor
- •Conditional Breakpoints and Actions
- •Temporary Breakpoints
- •Dependent Breakpoints
- •Dragging Breakpoints
- •Manage Breakpoints with Labels
- •Exporting Breakpoints
- •Using DataTips
- •Visualizing Complex Data Types
- •Bonus Tip
- •Using the Watch Window
- •The DebuggerDisplay Attribute
- •Evaluate Functions Without Side Effects
- •Format Specifiers
- •dynamic
- •hidden
- •results
- •Diagnostic Tools
- •CPU Usage
- •Memory Usage
- •The Events View
- •The Right Tool for the Right Project Type
- •Immediate Window
- •Attaching to a Running Process
- •Attach to a Remote Process
- •Remote Debugger Port Assignments
- •Remote Debugging
- •System Requirements
- •Download and Install Remote Tools
- •Running Remote Tools
- •Start Remote Debugging
- •Summary
- •Creating and Running Unit Tests
- •Create and Run a Test Playlist
- •Testing Timeouts
- •Using Live Unit Tests
- •Using IntelliTest to Generate Unit Tests
- •Focus IntelliTest Code Exploration
- •How to Measure Code Coverage in Visual Studio
- •Summary
- •Create a GitHub Account
- •Create and Clone a Repository
- •Create a Branch from Your Code
- •Creating and Handling Pull Requests
- •Multi-repo Support
- •Compare Branches
- •Check Out Commit
- •Line Staging
- •Summary
- •Index
Chapter 3 Debugging Your Code
dynamic
Displays the specified object using a Dynamic View
h
Hexadecimal integer
nq
String with no quotes
nse
Evaluates expressions without side effects where “nse” means “No Side Effects”
hidden
Displays all public and nonpublic members
raw
Displays item as it appears in the raw node. Valid on proxy objects only
results
Used with a variable that implements IEnumerable or IEnumerable<T>. Displays only members that contain the query result
You will recall that we used the “nq” format specifier with the DebuggerDisplay attribute discussed in a previous section.
Diagnostic Tools
Visual Studio gives developers access to performance measurement and profiling tools. The performance of your application should, therefore, be high on your priority list. An application that suffers from significant performance issues is as good as broken (especially from an end user’s perspective).
192

Chapter 3 Debugging Your Code
Visual Studio Diagnostic Tools might be enabled by default. If not, enable Diagnostic Tools by going to the Tools menu and clicking Options, Debugging, and then General. Ensure that Enable Diagnostic Tools while debugging is checked as seen in Figure 3-36.
Figure 3-36. Enable Diagnostic Tools
This will ensure that the Diagnostic Tools window opens automatically when you start debugging. When we start debugging our ShipmentLocator Windows Forms application, the Diagnostic Tools window will be displayed as seen in Figure 3-37.
193

Chapter 3 Debugging Your Code
Figure 3-37. Diagnostic Tools
With our Windows Forms application, you can use Diagnostic Tools to monitor memory or CPU usage as seen in Figure 3-38.
194

Chapter 3 Debugging Your Code
Figure 3-38. Select what to analyze
As you debug your application, you can see the CPU usage, memory usage, and other performance-related information.
CPU Usage
A great place to start your performance analysis is the CPU Usage tab. Place two breakpoints in your Form1_Load at the start and end of the function (Figure 3-39) and start debugging.
Figure 3-39. Setting breakpoints to analyze CPU usage
195

Chapter 3 Debugging Your Code
When the debugger reaches the second breakpoint, you will be able to view the summary profiling data for the debug session (as seen in Figure 3-40) at that point. To view detailed profiling data for the region of code you analyzed, click the Open details link under the CPU Usage tab.
Figure 3-40. CPU usage analysis results
As seen in Figure 3-41, you can view the functions by changing the Current View to Functions in the drop-down.
196

Chapter 3 Debugging Your Code
Figure 3-41. The Current View filtered by functions
If any of the functions in the CPU Usage pane seem to be problematic, double-click the function and then change the Current View to Caller/Callee to view a more detailed three-pane view of the analysis. As seen in Figure 3-42, the left pane will contain the calling function, the middle will contain the selected function, and any called functions will be displayed in the right pane.
197

Chapter 3 Debugging Your Code
Figure 3-42. Butterfly view of BtnLogin_Click
In the Current Function pane, you will see the Function Body section which details the time spent in the function body. As seen in Figure 3-43, you will also see the code represented for this function.
Figure 3-43. The code for the function
Because this excludes the calling and called functions, you get a better understanding of the function you are evaluating and can determine if it is the performance bottleneck or not.
198