What’s new in .NET Productivity

Mika Dumont

Mika

The .NET Productivity team (a.k.a. Roslyn) wants to help you be more productive! We’ve seen a lot of excitement in the past few months over our latest features which automate and reduce editing tasks to a single click and help save you time. In this post, I’ll cover some of the latest .NET productivity features available in Visual Studio 2019.

Tooling improvements

Starting in .NET 5.0, Roslyn analyzers are included with the .NET SDK. Roslyn analyzers are enabled, by default, for projects that target .NET 5.0 or later. You can enable them on projects that target earlier .NET versions by setting the EnableNETAnalyzers property to true. You can also use the Project Properties to enable/disable .NET analyzers. To access the Project Properties, right-click on a project within Solution Explorer and select Properties. Next, select the Code Analysis tab where you can either select or clear the checkbox to Enable .NET analyzers.

Project Properties dialog to enforce .NET 5.0 Analyzers

Another exciting feature is inline parameter name hints that inserts adornments for literals, casted literals, and object instantiations prior to each argument in function calls. In 16.9 Preview 1, we also added inline type hints for variables with inferred types and lambda parameter types. You’ll first need to turn this option on in Tools > Options > Text Editor > C# or Basic > Advanced and select Display inline parameter name hints and Display inline type hints. You can also use the shortcut Alt+F1 to briefly view hints.

Inline Hints

You can now extract members from a selected class to a new base class with the new extract base class refactoring. Place your cursor on either the class name or a highlighted member. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select Pull member(s) up to new base class or Extract base class. The new Extract Base Class dialog will open where you can specify the name for the base class and location of where it should be placed. You can select the members that you want to transfer to the new base class and choose to make the members abstract by selecting the checkbox in the Make abstract column.

Extract Base Class dialog

Code cleanup has new configuration options that can apply formatting and file header preferences set in your EditorConfig file across a single file or an entire solution.

Code Cleanup dialog

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). The following list shows the code fixes and refactorings that are new in Visual Studio 2019:

The inline method refactoring helps you replace usages of a static, instance, and extension method within a single statement body with an option to remove the original method declaration. Place your cursor on the usage of the method. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Next select from one of the following options:

Select Inline <QualifiedMethodName> to remove the inline method declaration:

Inline method refactoring removing method declaration

Select Inline and keep <QualifiedMethodName> to preserve the original method declaration:

Inline method refactoring preserving method declaration

The use pattern matching refactoring introduces the new C# 9 pattern combinators. Along with the pattern matching suggestions, such as converting == to use is where applicable, this code fix also suggests the pattern combinators and, or and not when matching multiple different patterns and negating. Place your cursor inside the statement. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Use pattern matching.

Use pattern matching refactoring

The make class abstract refactoring allows you to easily make a class abstract when you’re trying to write an abstract method in a class that isn’t abstract. Place your cursor on the method error. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Make class ‘abstract’.

Make class abstract refactoring

The convert typeof to nameof refactoring allows you to easily convert instances of typeof(<QualifiedType>).Name to nameof(<QualifiedType>) in C# and instances of GetType(<QualifiedType>).Name to NameOf(<QualifiedType>) in Visual Basic. Using nameof instead of the name of the type avoids the reflections involved when retrieving an object. Place your cursor within the typeof(<QualifiedType>).Name. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Convert ’typeof’ to ’nameof’.

Convert typeof to nameof refactoring

Visual Basic had multiple ways of passing parameters, ByVal and ByRef, and for a long time ByVal has been optional. We now fade ByVal to say it’s not necessary along with a code fix to remove the unnecessary ByVal. Place your cursor on the ByVal keyword. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select ‘ByVal’ keyword is unnecessary and can be removed.

Remove unnecessary ByVal keyword

Now, there’s also a code fix to remove the in keyword where the argument shouldn’t be passed by reference. Place your cursor on the error. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Remove ‘in’ keyword.

Remove in keyword

In 16.9 Preview 1, we also added a code fix that removes redundant equality expressions for both C# and Visual Basic. Place your cursor on the redundant equality expression. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Remove redundant equality.

Remove redundant equality

And the last refactoring we added in 16.9 Preview 1 suggests using ‘new(…)’ in non-contentious scenarios. Place your cursor on the field declaration. Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Use ‘new(…)’.

Use new() refactoring

Get involved

This was just a sneak peek 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. You can also share your feedback with us on GitHub or tweet @roslyn, we’d love to hear what you think!

10 comments

Leave a comment

  • Avatar
    David N

    Could you update the name of the team please? .net productivity sounds like it should cover all the core .net languages at least, but it’s only the C# and VB subsets. It feels exclusionary.

    Thanks,

    An F# on .net developer.

  • Avatar
    James Lonero

    The redundant equality expression checks are helpful, can you add one for a check of null. For example:
    if (obj == null)
    {
    obj = new object();
    }
    :
    I have seen a lot of these and sometime, there are programming errors introduce when we accidently type: (obj = null)
    Thanks.

  • Avatar
    Rod Falanga

    Mika, I love these new additions for VS 2019. Especially the removing redundant equality. It’s not the most important enhancement, but I think it will help as the code I’ve been seeing a lot of the last 5 years tends to use a lot of that redundant equality.

    I’ve got a question about the first thing you mentioned, the new Rosyln analyzers. Where I work, we use .NET 4.5.2 for all new applications. I don’t understand why and have asked about it. I’ve not been given an answer – it just is. Anyway, recently I upgraded my VS 2019 to 16.8. Once I did that, then a new application I’ve been working on for the last 3 months with a team of other developers, started to raise lots of what I would consider inconsequential errors. Errors such as a parameter to a method started with the wrong case for the name of the parameter. It shouldn’t have been lowercase but was. That was enough, along with 1600+ other errors like that, to fail the build, since all 1600+ were identified as errors. I opened a help ticket with Microsoft to get help on this. I wasn’t even aware of this dialog in the project’s Properties under the Code Analysis tab, until the help tech showed it to me. For now, I’ve cleared the checkboxes for both Run on build and Run on live analysis. That fixes my problem, but I wonder a few things.

    First, I assume but want to verify, does clearing those two checkboxes only affect that project? I sure hope the answer is yes. And why did having those two checkboxes checked cause a .NET 4.5.2 to fail so miserably, when before it was fine?

    The second question is about the EnableNETAnalyzers property. Checking its checkbox. I am not clear if I should, or shouldn’t, check that checkbox (set it to true), in my work environment. If I check it, will it automatically bring back all of those errors? If it doesn’t, then how does it help me and my coworkers?