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.
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.
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.
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 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:
Select Inline and keep <QualifiedMethodName>
to preserve the original 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.
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’.
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’.
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.
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.
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.
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(…)’.
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!
Hi @Mika Dumont
Thanks for comprehensive update.
Our open source project has been collecting Visual Studio issues our users have encountered in the past few years, which we have logged on with VS team. The list is available here. https://github.com/unoplatform/uno/issues/982 . Are there any plans to address them, especially the top 3 which are huge blockers for us:
Thanks so much
Sasha
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...
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.
This sounds like this issue This is a bug and we should fix it :) The intent is that you do not get new errors or warnings on existing projects that...
Cool! Thanks for hints and editor configurations.
Why does jsonelement not add a string indexer?
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.
@JamesLonero I think you’re confusing C# with another language.
is illegal in C# for this very reason. (if statements require booleans)
Does last refactoring New(…) work for VB to convert to “=“ to “As” if not it should.
Visual Studio is a great productivity tool! Please keep adding new features 😉
https://developercommunity.visualstudio.com/idea/1262231/productivity-feature-add-logger-to-class.html
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.
Quick note: The inline hints shortcut key given above (Alt+F1) is incorrect for VS 16.9p1. It’s Ctrl+Alt.
Hi Clinton! The shortcut used to be Ctrl+Alt but we recently changed it to Alt+F1.