What’s new in .NET Productivity with Visual Studio 2022

Mika Dumont

With the release of Visual Studio 2022 the Roslyn team continues to enhance your .NET developer productivity with the latest tooling improvements.

In this post I’ll cover the following .NET productivity enhancements:

  • Navigate to source code
  • Stack Trace Explorer
  • Naming Styles in the EditorConfig UI
  • Sync namespaces from Solution Explorer
  • IntelliSense completion for await
  • New Code Fixes and Refactorings

Navigating and exploring code is an integral part of developer productivity. In Visual Studio 2022 we now surface embedded source and Source Link as part of Go To Definition. This allows you to navigate to original source files that declare the target symbol that isn’t in your current solution. Place your cursor on a symbol and press F12 to navigate to the original source code.

Source Link Navigation

A new Stack Trace Explorer Window displays stack traces allowing you to click and navigate directly to the related code. You can copy a stack trace from your solution and paste it into the Stack Trace Explorer window to get started. To open the Stack Trace Explorer window, go to View > Other Windows and select Stack Trace Explorer or use the shortcut Ctrl+E, Ctrl+S.

Stack Trace Explorer

We added a new command called Value Tracking allowing you to perform data flow analysis on your code to help you quickly determine how certain values might have passed at a given point. Value Tracking is available on any member in the context (right-click) menu by selecting the Track Value Source command. The Track Value Source command will open the Value Tracking window allowing you to analyze results.

Value Tracking

We added an option to underline variables that are reassigned. This is off by default so you will need to enable it in Tools > Options > Text Editor > C# or Basic > Advanced and select Underline reassigned variables. This helps reassigned variables stand out from other variables in the same local scope.

Underline reassigned variables

The Code Definition Window now supports C# and Visual Basic allowing you to quickly understand and explore code. To use the Code Definition Window, select View > Window > Code Definition. Next, place your cursor on an identifier to navigate and explore code.

The Code Definition Window

In C# 8.0 we introduced nullable reference types allowing you to declare whether null is expected. To use nullable reference types you either need to add the <Nullable>enable</Nullable> element to your project file or add the #nullable enable pragma to every source file in your project. To help streamline this process we now automatically include the <Nullable>enable</Nullable> for new .NET projects. For existing .NET projects that target .NET Core 3.1 or newer, we offer a new refactoring to enable nullable reference types across a project. Place your cursor on a #nullable enable pragma and press (Ctrl+.) to trigger the Quick Actions and Refactorings menu. Select Enable nullable reference types in a project.

Enable nullable refactoring

IntelliSense completion

IntelliSense is a code-completion aid that includes a number of features: List Members, Parameter Info, Quick Info, and Completion. We recently added IntelliSense completion for await within an awaitable expression. Start typing an awaitable expression and notice how await will now show up in the completion list.

Async completion

If you want to learn more about recent additions to IntelliSense including AI helping you code, check out the IntelliCode docs.

Code fixes & refactorings

Visual Studio provides hints to help you maintain and modify your code in the form of code fixes and refactorings. These appear as lightbulbs and screwdrivers next to your code or in the margin. The hints can resolve warnings and errors as well as provide suggestions. You can open these suggestions by typing (Ctrl+.) or by clicking on the lightbulb or screwdriver icons.

You can check out the most popular refactorings that are built in to Visual Studio at https://aka.ms/refactor. We’ve added a bunch of new code fixes and refactorings in Visual Studio 2022! Here are some of our favorites.

  • Introduce parameter
  • File scoped namespace
  • Use tuple to swap values
  • Move static members
  • Simplify property pattern
  • Prefer null check over type check
  • Sync namespaces

The introduce parameter refactoring will move an expression from a method implementation to its callers by adding a new parameter. Place your cursor on the line containing the expression or highlight the expression. Press (Ctrl+.) to trigger the Quick Actions and Refactorings menu. Select Introduce parameter for {0} or Introduce parameter for all occurrences of {0}. Both options will have three flyout options to either (1) insert the updated expression at all the call sites, (2) extract and create a new method that returns the expression and adds an argument at the call sites, or (3) create an overload of the method that contains the expression and calls upon the original method.

Introduce parameter refactoring

In C# 10.0 we introduced file-scoped namespace so you no longer need to nest class definitions within a namespace. To use file-scoped namespace, make sure your project targets the .NET 6.0 SDK or you can set the language version in your project file to 10.0. Place your cursor on a namespace. Press (Ctrl+.) to trigger the Quick Actions and Refactorings menu. Select Convert to file-scoped namespace.

File scoped namespace dark theme

There is a new refactoring that detects variable swaps and suggests using a tuple to swap values instead of using a temporary variable in-order to swap arguments. Place your cursor on a temporary variable assignment where you are swapping values. Press (Ctrl+.) to trigger the Quick Actions and Refactorings menu. Select Use tuple to swap values.

Use tuple to swap values refactoring

You can more easily move static members to a new type. Place your cursor on a static member. Press (Ctrl+.) to trigger the Quick Actions and Refactorings menu. Select Move static members to another type. This will open a dialog where you can select the members that you would like to move.

Move static members to another type refactoring

The simplify code to use the new C# 10.0 extended property patterns refactoring reduces noise allowing you to reference nested members instead of nesting another recursive pattern. Place your cursor on a nested member reference. Press (Ctrl+.) to trigger the Quick Actions and Refactorings menu. Select Simplify property pattern.

Simplify property pattern refactoring

There is now a refactoring to prefer is not null over is object when applied to value types. To use this new refactoring place your cursor on a type check. Press (Ctrl+.) to trigger the Quick Actions and Refactorings menu. Select Prefer ‘null’ check over type check.

Prefer null check over type check refactoring

You can also invoke code fixes and refactorings from the Solution Explorer (right-click) menu. One of our most popular refactorings is sync namespaces allowing you to synchronize namespaces to match your folder structure. The Sync Namespaces command is now available in the (right-click) menu of a project in Solution Explorer. Selecting Sync Namespaces will automatically synchronize namespaces to match your folder structure.

Sync namespaces refactoring

Code style enforcement

Enforcing consistent code style is important as developer teams and their code bases grow. You can configure code styles with EditorConfig! EditorConfig files help to keep your code consistent by defining code styles and formats. These files can live with your code in its repository and use the same source control. This way the style guidance is the same for everyone on your team who clones from that repository. With EditorConfig files you can enable or disable individual .NET coding conventions and configure the severity to which you want each rule enforced.

In Visual Studio 2019 we introduced a new UI for EditorConfig allowing you to easily view and configure every available .NET coding convention. In this release we added Naming Styles to the EditorConfig UI. Naming Styles allow you to enforce naming conventions in your code such as interfaces should always start with the letter “I”. To add an EditorConfig file to a project or solution: 

  • Right-click on the project or solution name within the Solution Explorer.
  • Select Add New Item.
  • In the Add New Item dialog, search for EditorConfig.
  • Select the .NET EditorConfig template to add an EditorConfig file prepopulated with default options.

Naming Styles

You can also add a new EditorConfig file from the command line by typing dotnet new editorconfig. To use dotnet new editorconfig make sure your project targets the .NET 6.0 SDK or later. Next, open the Visual Studio integrated terminal by pressing (Ctrl+`). You can then run dotnet new editorconfig to add a new EditorConfig file to your project.

Thank you

Last, but certainly not least, a big Thank You to the following people who contributed to Roslyn. Ultimately, you’ve helped make Visual Studio 2022 awesome.

Get involved

This was just a sneak peak of what’s new in Visual Studio 2022. For a complete list of what’s new, see the release notes. And feel free to provide feedback on the Developer Community website, or using the Report a Problem tool in Visual Studio.