Catch up on the latest .NET Productivity features

Mika Dumont

The Roslyn team continuously works to provide tooling that deeply understands the code you are writing in-order to help you be more productive. In this post, I’ll cover some of the latest .NET Productivity features available in Visual Studio 2019.

Tooling improvements

The feature that I’m most excited about is the new Go To Base command. Go To Base allows you to easily navigate up the inheritance chain. The command is available on the context (right-click) menu of the element that you want to navigate the inheritance hierarchy. Or you can press Alt+Home.

Go To Base

Find All References now categorizes the results by type and member. You can group by type and member in the Find All References window.

Find All References

Code fixes and refactorings

Code fixes and refactorings are the code suggestions the compiler provides through the light bulb and screwdriver icons. To trigger the Quick Actions and Refactorings menu, press (Ctrl+.) or (Alt+Enter). Below are the code fixes and refactorings that are new in Visual Studio 2019:

The extract local function refactoring allows you to turn a fragment of code from an existing method into a local function. Highlight the code that you want extracted. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Extract local function.

Extract Local Function

The make members static code fix helps improve readability by making a non-static member static. Place your cursor on the member name. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Make static.

Make Member Static

The simplify string interpolation refactoring simplifies string interpolations to be more legible and concise. Place your cursor on the string interpolation. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Simplify interpolation.

Simplify String Interpolation

The convert if statements to switch statements or switch expressions refactoring enables an easy transition between if statements and switch statements or expressions. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Convert to ‘switch’ statement or Convert to ‘switch’ expression.

Convert if to switch statement or expression

The make local function static refactoring allows you to make a local function static and pass in variables defined outside the function to the function’s declaration and calls. Place your cursor on the function name. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Make local function ‘static’.

Make Local Function Static

The pass variable explicitly in a local static function refactoring gives you flexibility to define variables outside a context, but still be able to pass them in as arguments to the static local function. Place your cursor on the variable within the static local function. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Pass variable explicitly in local static function.

Pass Variable Explicitly Static Local Function

The add null checks for all parameters refactoring saves you time by automatically adding if statements that check the nullity of all the nullable, non-checked parameters. Place your cursor on any parameter within the method. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Add null checks for all parameters.

Add Null Checks for All Parameters

The introduce a local variable refactoring allows you to immediately generate a local variable to replace an existing expression. Highlight the expression that you want to assign to a new local variable. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Introduce local for.

Introduce Local Variable

Get involved

This was just a sneak peak of what’s new in Visual Studio 2019. 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.

3 comments

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

  • Marcel Beck - RothSoft 0

    Hi,

    Quick Actions like “Add null checks for all parameters” are super useful. Thx for this. That said, i have a question here:

    Is there any setting in VS, to control HOW the needed namespace for the Quick Fix code (in your example: all the “throw new System.ArgumentExce…” lines) is inserted ?

    In example your code above shows all lines with “System.”, whereas the direct link of your feature above (https://docs.microsoft.com/en-us/visualstudio/ide/reference/add-null-checks-for-parameters?view=vs-2019) shows it without, like this: “throw new ArgumentExce…” (which means “import System;” clause is needed at the top of the file).

    Can i control, via a VS setting, if all Quick Actions insert their code either with

    import System;
    throw new ArgumentException();

    or with

    throw new System.ArgumentException();

    ???

    Ofc, this is not just a question about this Quick Action, but about all Quick Actions, that insert stuff, that is in a explicit namespace.

    So, is there any setting in VS, to control how needed namespaces are handled for code inserted by a Quick Action ?

    Because its somewhat stressy, to go through all the “System.Argument…” lines and either delete “System.” and add “import System;” at top of file, or to go through all the lines and click “ALT + ENTER” (or “STRG + .”) to quick fix it with “Add import” Quick Action.

    Would be super nice, if VS has a setting with a checkbox looking like this: “Automatically add import statement, when needed, for Quick Actions”. If we check that setting, all Quick Actions insert a “import xxx;” statement at top of the file (if needed). If we uncheck that setting, all namespaces are directly inserted in all lines of the inserted code (if needed).

    • Assil Abdulrahim 0

      VS will add the import for you.
      But I am not sure if it adds it in the same click of QuickAction or it requires addition QuickAction.

  • Dean Jackson 0

    What about this bug, which has been sitting with the “Help Wanted” Github label for months?

    https://github.com/dotnet/roslyn/issues/35943

Feedback usabilla icon