Skip to main content

What’s new in .NET Productivity

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!

The post What’s new in .NET Productivity appeared first on .NET Blog.



source https://devblogs.microsoft.com/dotnet/whats-new-in-net-productivity/

Comments

Popular posts from this blog

Fake CVR Generator Denmark

What Is Danish CVR The Central Business Register (CVR) is the central register of the state with information on all Danish companies. Since 1999, the Central Business Register has been the authoritative register for current and historical basic data on all registered companies in Denmark. Data comes from the companies' own registrations on Virk Report. There is also information on associations and public authorities in the CVR. As of 2018, CVR also contains information on Greenlandic companies, associations and authorities. In CVR at Virk you can do single lookups, filtered searches, create extracts and subscriptions, and retrieve a wide range of company documents and transcripts. Generate Danish CVR For Test (Fake) Click the button below to generate the valid CVR number for Denmark. You can click multiple times to generate several numbers. These numbers can be used to Test your sofware application that uses CVR, or Testing CVR APIs that Danish Govt provide. Generate

How To Iterate Dictionary Object

Dictionary is a object that can store values in Key-Value pair. its just like a list, the only difference is: List can be iterate using index(0-n) but not the Dictionary . Generally when we try to iterate the dictionary we get below error: " Collection was modified; enumeration operation may not execute. " So How to parse a dictionary and modify its values?? To iterate dictionary we must loop through it's keys or key - value pair. Using keys

How To Append Data to HTML5 localStorage or sessionStorage?

The localStorage property allows you to access a local Storage object. localStorage is similar to sessionStorage. The only difference is that, while data stored in localStorage has no expiration time untill unless user deletes his cache, data stored in sessionStorage gets cleared when the originating window or tab get closed. These are new HTML5 objects and provide these methods to deal with it: The following snippet accesses the current domain's local Storage object and adds a data item to it using Storage.setItem() . localStorage.setItem('myFav', 'Taylor Swift'); or you can use the keyname directly as : localStorage.myFav = 'Taylor Swift'; To grab the value set in localStorage or sessionStorage, we can use localStorage.getItem("myFav"); or localStorage.myFav There's no append function for localStorage or sessionStorage objects. It's not hard to write one though.The simplest solution goes here: But we can kee