December 15th, 2021

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

Mika Dumont
Senior Product Manager

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

Navigation and code exploration

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.

Author

Mika Dumont
Senior Product Manager

Mika is a Product Manager on the .NET and GitHub Copilot developer experience.

25 comments

Discussion is closed. Login to edit/delete existing comments.

  • ericmutta

    It’s 21/Jan/2022 and I have Visual Studio 2022 Community v17.0.5. None of these features work (e.g. no “stack trace explorer” window, and the “await” Intellisense doesn’t work). Which version of Visual Studio 2022 is required for these to work?

  • André Köhler · Edited

    "Go To Definition" to my own NuGet package with embedded source files does not work for me. VS2022 only shows the function headers. My library was built with the "EmbedAllSources" property set to "true" in the csproj file and
    "dotnet pack --configuration Release --include-symbols"

    Is there anything else I have to do to make this feature work?
    My aim is to publish the NuGet package to our companies private NuGet directory on our own file server.

    Read more
  • John B

    Sync Namespaces also seems like a bad idea in general, since the folder structure serves Solution Explorer while namespaces serve IntelliSense type-ahead. Fine grained name spaces mean type-ahead seldom works. I’d rather not offer easy support that paradigm in the IDE.

  • Vincent Thorn

    6 July 2016 I created proposal "Intellisense: Insert full method call". 5.5 YEARS(!!!!) past in stupid blah-blah and MS just now included feature in 17.2 milestone. Are you serious?! 5.5 years you discussing "do we need full method completion"?? If I were your boss, you'll be fired in 1 year for wasting company resources and time.
    Feature I suggested affects EVERY SINGLE CODER at every single minute of typing in IDE. What can be more...

    Read more
    • Vincent Thorn

      And hell, remove ban from WrongBit – it’s me who gave you idea what you MUST implement! But because you annoy me by your stupidity and make nervous, I wrote what I think about MS team. It’s my freedom of speech, you cannot ban me for that! YOU are guilty you have so unqualified team (who even offered _ME_ to implement feature, instead of doing by MS team!!!).

  • Hakan

    Thank you Mika for this great article,

    I think the “Code Definition” window must be under View > Code Definition
    but in the article, it is View > Window > Code Definition.

    • Mika DumontMicrosoft employee Author

      Yes, thank you for catching that!

  • WM Z

    I noticed that you had created `ConsoleApp335` in the first video.
    Why not bring back the “Save new projects when created” option?

  • Gvozden Radičević

    Why SQL debugger can’t step-into through VS2022 ?
    In contrast, the VS 2019 works correctly.

  • Mike M

    @Mika Dumont Thanks for continuing effort of making Visual Studio more production.

    However, would like to escalate to you attention growing number of reports regarding high memory consumption at Visual Feedback Feedback. Given the high number of reports, it is a issue worth investigation and resolving.

    • WM Z

      Previously I investigated the memory consumption problem of VS 2022 and found that part of its memory leak could be attributed to unreleased WPF's `AutomationPeer` instances. As a result, document views can not be released, and the huge Roslyn semantic results (code document objects) behind the view. Since VS is now 64bit, itself uses more memory and the leak is almost doubled versus VS 2019 or prior versions.

      Here's my finding about the leak:
      https://github.com/wmjordan/Codist/issues/169#issuecomment-942860808

      And a...

      Read more
  • Zehev Spitz · Edited

    I can’t get the Code Definition Window to work with C#.

    Also, as noted in another comment, the Stack Trace Explorer is not accessible from the menus nor from Quick Search; and it also is not available in the list of Commands (Tools -> Options… -> Environment -> Keyboard).

    • Mika DumontMicrosoft employee Author

      Hi Zehev! The Code Definition Window was released in 17.1 Preview 1. Can you validate that you are on version 17.1 Preview 1?

      • Zehev Spitz

        That would explain it — I’m on 17.0.4. It might be a good idea to clarify that in the article. Both Code Definition Window for C#/VB and Stack Trace Explorer look like features that I would use very heavily.

      • Karel Novak · Edited

        It’s confusing, Code Definition Window is in version 17.0.4 (release) too.

  • Henok Girma

    I can’t find the Stack Trace Explorer. I have the latest VS 2022. I tried the shortcut or looking for in View > Other Windows and it’s not there.

    • Mika DumontMicrosoft employee Author

      Hi Henok! Stack Trace Explorer will be available in 17.1 Preview 2. I will keep you posted on when 17.1 Preview 2 officially releases so you can give Stack Trace Explorer a try!