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

Mika

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.

25 comments

Leave a comment

  • Muhammad Miftah

    After all these years, can you at least implement auto-completion of the parentheses pair when entering a method name?
    “File.ReadAllTextAsync”; why do I still have to enter the “()” manually? The vast majority of the time, when a method name is typed, it’s to invoke it().

    • Vincent Thorn

      +1, even me personally made proposal to put full call (inc. “;”). Nobody even bothered with that – they are busy with more important BS.
      Seems C# team is a bunch of indian students who works on their own. Has MS at least 5 talented developers???

  • Vincent Thorn

    Why language improvements (C# 9.0+) are NOT available for .NET Framework projects? What the hell so important and unique you have in Core (and don’t have in FW) that you NOT implemented “file-scoped namespace”? (feature which costs you literally nothing to implement)

    • Richard Lawley

      This seems to be a common misconception – a lot of posts on it (including this one!) states that the language improvements require .NET 6. This is incorrect – all they require is setting langversion to 10 in your project file. I’ve successfully migrated a large .NET 4.7.2 project to file-scoped namespaces and global usings.

      The main thing which is different with .NET 6 is that these are enabled by default – targeting .NET 6 implicitly sets langversion to 10, which means you don’t need to add the langversion tag.

      • Zehev Spitz

        Some features require .NET CLR support, such as default interfaces; setting the language version won’t make it possible to use those features.
        Some other features require various shims, such as nullable attributes or records; it’s possible to add the necessary types by hand to fill in the blanks.

      • Ken Kasajian

        You’re ignoring the fact that Microsoft explicitly disabled the user’s ability to change the language version in the settings when we used to be able to do it. So it looks like it can’t be done. You had to google around and find articles on how it can be enabled. Most people aren’t going to do that. Plus doing so seems like you’re doing something non-standard, which is exactly the reason it was done: to reduce the overhead of testing and supporting newer C# features in .NET Framework. Some languages features won’t even work. So they’d have to have a way deal with that.
        It’s just easier for MS to say not supported because they hardly want anyone to remain on .NET Framework.

  • Fabien Geraud

    The Sync Namespaces command seems a bit dangerous. There is a lot of case i’m not how would it work :
    – Class conflict.
    – Not loaded project or project in another sln
    – Intended case
    I prefer a rule in StyleCopAnalyzers.

    For code style enforcement. It’s great but with style from the .ruleset and from .editorconfig it’s sometime hard to keep both in sync. It may be great to have one file to configure all code style. Or at least avoid overlap. The usage of ‘var’ instead on the name can be set on both and in different way.

    I hope this improvement will be in VSCode 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

      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!

  • Zehev Spitz

    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).

      • 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.

  • 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

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

  • 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.

  • 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 important than this?!?! Productivity is #1 task which you worry about! Not icons, but productivity, hey!

    And even now, if feature will be implemented, I’m 200% sure it will be done most idiotic way. I suggested “you type object.fu”, then intellisense popup appears, you select “function” and by ENTER you have in code “object.function();”. I simply GUARANTEE that MS will screw up this idea and do something different, remember my words.

    • 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!!!).