
- •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 2 Working with Visual Studio 2022
Figure 2-6. Filter project templates
You are, therefore, able to quickly find the most suitable project template.
Take note, though, that you may need to install a workload if you do not find the project template you want. To do this, click Install more tools and features. Refer to Chapter 1 to see how to use workloads in Visual Studio.
Of the several project templates available, let’s see which ones there are and what project is suitable for specific situations.
Various Project Templates
You can choose from a host of project templates in Visual Studio 2022. I would even go as far as to say that it’s now even easier to find the template you need to use due to the filters in the Create a new project window. Let’s have a look at a few of these project templates next.
69

Chapter 2 Working with Visual Studio 2022
Console Applications
I remember the first time I wrote a single line of code. I used a Console Application, a great template to use when you don’t need a UI for your application. The Console
Application project template running on the .NET Framework is displayed in Figure 2-7.
Figure 2-7. Console App (.NET Framework)
You shall notice that this application is suited for running on Windows machines. But what if you need to run the Console Application across platforms such as Windows, Linux, and macOS? Here is where .NET 6 comes into play.
The Console Application project template running on .NET 6 is displayed in Figure 2-8.
Figure 2-8. A .NET 6 Console App
A few years ago (long before .NET 6 was ever a thing), I needed to create an application that could be triggered on a schedule. The application’s executable would then be passed one of several parameters to determine which database to connect to.
The application had to run without any user intervention to perform some sort of maintenance task. Because no user intervention was needed, a Console Application best suited the use case. Be aware that a Console Application can accept user input, but for my purposes with this application, it was not necessary.
70

Chapter 2 Working with Visual Studio 2022
Windows Forms Application
In contrast to the Console Application, the Windows Forms application template is used when you need to create an app that has a UI. The project template (like the Console Application) can run on the .NET Framework or .NET 6, as seen in Figure 2-9.
Figure 2-9. Windows Forms App (.NET 6 and .NET Framework)
It is important to note that before .NET 6, there were two separate products: the
.NET Framework and .NET Core. If you’re part of the Old Guard, you will have worked extensively with the .NET Framework. With .NET Core, however, a process was set in motion that would change the face of .NET development forever. Being open source,
.NET Core was rewritten from scratch and is designed to work across all platforms, such as Windows, Linux, and Mac.
Now, with .NET 6.0 going forward, developers will have just a single version of .NET (and it’s just called .NET). Think of .NET 6 as a unified development platform that allows developers to create desktop, cloud, web, mobile, gaming, IoT, and AI applications.
Windows Service
If you ever need to create a Windows application that continually runs in the background, performing some specific task, your best choice would be to use a Windows Service template as seen in Figure 2-10.
Figure 2-10. Windows Service (.NET Framework)
71

Chapter 2 Working with Visual Studio 2022
Imagine that the application needs to monitor specific activity (be that in a database or file system) and then write messages to an event log. A Windows Service is ideally suited for this purpose.
Windows Services have an OnStart method that allows you to define what needs to happen when the service starts. By definition, Windows Services are long-running applications that need to poll or monitor the system it runs on. You will need to use a Timer component to enable the polling functionality.
A common mistake is to use a Windows Forms Timer for a Windows Service. You must ensure that you use the timer in the System.Timers.Timer namespace instead.
The System.Timers timer (Figure 2-11) that you add to the Windows Service will raise an Elapsed event at specific intervals.
Figure 2-11. Various timer namespaces
In this Elapsed event, you can write the code your service needs to run to do what it needs to do.
72