Learn about the latest .NET Productivity features

Mika

The .NET Productivity team (a.k.a. Roslyn) is constantly thinking of new ways to make .NET developers more productive. We’ve been working hard to take the feedback you’ve sent us and turn it into tools that you want! 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 IntelliSense completion in DateTime and TimeSpan string literals. This feature is extremely helpful because we all know remembering DateTime and TimeSpan formats is hard enough. Place your caret inside the DateTime or TimeSpan string literal and press (Ctrl + Space). You’ll then see completion options and an explanation as to what each character means.IntelliSense Completion DateTime TimeSpan


Add file header allows you to easily add file headers to existing files, projects, and solutions using EditorConfig. You’ll first need to add the file_header_template rule to your .editorconfig file. Then, set the value to equal the header text you’d like applied. Next, place your caret on the first line of any C# or Visual Basic file. Press (Ctrl+.) to trigger the Quick Actions and Refactorings menu and select Add file header.

Add File Header


The change method signature dialog now allows you to add a parameter. Place your caret within the method’s signature. Press (Ctrl+.) to trigger the Quick Actions and Refactorings menu and select Change signature. The following dialog will open where you can now select Add to add a parameter.

Change Signature


Once you select Add, the new Add Parameter dialog opens. The Add Parameter dialog allows you to add a type name and a parameter name. You can choose to make the parameter required or optional with a default value. You can then add a value at the call site and choose a named argument for that value or you can introduce a TODO variable. The TODO variable puts a TODO in your code so you can visit each error and go through each call site independently and decide what to pass. For optional parameters you have the option to omit the call site completely.

Add Parameter


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 add explicit cast code fix allows you to add an explicit cast when an expression cannot be implicitly cast. Place your caret on the error. Press (Ctrl+.) to trigger the Quick Actions and Refactorings menu and select Add explicit cast.

Add Explicit Cast


The simplify conditional expression refactoring simplifies conditional expressions to be more legible and concise. Place your caret on the conditional expression. Press (Ctrl+.) to trigger the Quick Actions and Refactorings menu and select Simplify conditional expression.

Simplify Conditional Expression


Have you ever wished you could easily read or convert to a verbatim string? Now you have a refactoring at your fingertips to convert between regular string and verbatim string literals. Place your caret on either the regular string or the verbatim string literal. Press (Ctrl+.) to trigger the Quick Actions and Refactorings menu. Next, select from one of the following:

Select Convert to verbatim string:

Convert To Verbatim String


Select Convert to regular string:

Convert To Regular String


The add debugger display attribute refactoring allows you to pin properties within the debugger programmatically in your code. Place your caret on the class name. Press (Ctrl+.) to trigger the Quick Actions and Refactorings menu and select Add ‘DebuggerDisplay’ attribute. This will add the debugger display attribute to the top of your class and generate an auto method that returns ToString(), which you can edit to return the property value you want pinned in the debugger.

Add Debugger Display Attribute


The generate comparison operators refactoring generates a boilerplate code with comparison operators for types that implement IComparable. Place your caret either inside the class or on IComparable. Press (Ctrl+.) to trigger the Quick Actions and Refactorings menu and select Generate comparison operators.

Generate Comparison Operators


The generate IEquatable operators refactoring automatically adds the IEquatable as well as the equals and not equals operators for structs. Place your caret within the struct. Press (Ctrl+.) to trigger the Quick Actions and Refactorings menu and select Generate Equals(object).

Generate IEquatable Operators


The generate properties when generating a constructor allows you to easily create a constructor with properties in a type. Place your caret on the instance. Press (Ctrl+.) to trigger the Quick Actions and Refactorings menu and select Select Generate constructor in <QualifiedName> (with properties).

Generate Properties With Constructor


There’s now an easy fix for accidental assignments and comparisons. Place your caret on the warning. Press (Ctrl+.) to trigger the Quick Actions and Refactorings menu. Next, select from one of the following options:

For accidental assignments, select Assign to ‘<QualifiedName>.value’:

Accidental Assignments


For accidental comparisons, select Compare to ‘<QualifiedName>.value’:

Accidental Comparison


The null suppression operator warning and code fix helps you to easily identify and fix a suppression operator that has no effect. For example, in this case someone wanted to express that something isn’t `string` and typed `!is string` instead of `is not string`. The `!` is legal but is interpreted as asserting the expression on the left as is not `null`. Since that can be confusing, we now offer a warning and code fix. Place your caret on the suppression operator. Press (Ctrl+.) to trigger the Quick Actions and Refactorings menu. Next, select from one of the following:

To remove the operator completely, select Remove operator (preserves semantics):

Remove Suppression Operator


To negate the expression, select Negate expression (change semantics):

Negate Expression


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.

33 comments

Comments are closed. Login to edit/delete your existing comments

  • Jason Brower

    I am very pleased to see more and more refactoring features come to Visual Studio out of the box. I spend a significant amount of time refactoring and I hope to continue to see those features improved. I am sure there are plenty I am not aware of, but I would love to see a feature like right click on a folder to add file headers to all files in the folder, or to adjust all namespaces to match the directory structure and we need for XAML files to be adjusted as well. Rename refactoring really isn’t useful if you work with XAML, your better off using find and replace or following up your refactor with a find and replace. XAML tooling really hasn’t improved greatly in the 13 years I have been using it, although hot reload was a pretty significant game changer. I can’t use it though because I am writing in WinUI and its not compatible with Visual Studio’s designer or intellisense.

    Keep the features coming, I do appreciate the changes.

  • Lucas R. Vogel

    I’ve used Visual Studio for over 20 years, and I have to say that since switching to JetBrains Rider on Linux Mint, I’ve been far more productive (and proficient) writing code than I’ve ever been. It’s not without its minor quirks here and there, but the IDE feels so natural for most things that it’s become a burden to use anything else. Many of the features discussed in this article have already been a part of Rider.

    Definitely worth having an individual subscription for.

  • antony.male@gmail.com

    Those generated comparison operators look wrong. What happens if left is null?

  • Tobias Klimm

    Refactorings are great.

    I support that 100%

    But 😉

    it would help so much to have keyboardshortcuts to directly execute the refactorings and not go over the context menu called via ctrl+.
    like if the cursor pointed on a local variable just hit ctrl+r+p i can make it a property or simply inline a variable without the quick actions menu just with a keyboardshortcut

  • zig zag

    UX of Add Parameter dialog is bad. It looks like something from early 2000s with too much clicking/tabbing to do a very simple task.

    There are 3 text boxes and a radio buttons list that “help” user basically write either:

    type name

    or

    type name = value

    Everything from Parameter information & Parameter kind groups could be merged into a single text box with a pre-filled snippet (eg. “type name”). Add IntelliSense and validation to that single text box and you’ll have much nicer UX with less visual overload and faster input.

    Other than that, great work! I really like productivity features like these. They make coding simpler and more enjoyable.

  • Andy A

    Hello everyone, I’m from China. Nice to meet you. I’m a novice. I’ll give you more advice!

  • Heinrich Moser

    These new refactorings look great, thanks!

    One thing I’ve noticed, though: All the code examples are in C#. Yes, we got the message you’ve subtly been sending out for the past few years, and we are in the process of switching to C#, but for our legacy VB.NET projects it would be helpful to know which of those new refactorings are available for VB.NET and which ones are C# only.

    I image a short line “Available for: C# and VB.NET.” or “Available for: C#.” (or even “Available for: VB.NET.”, although I doubt this will ever happen) would fit nicely below each heading. Please consider this as an improvement suggestion for future announcements of IntelliSense improvements.

  • Tudor Turcu

    So slowly, many years later, Visual Studio is catching up what Resharper has offered long time ago.. 🙂

  • Paul Stancer

    Don’t mean to be picky. But how about you fix the SQL editor in VS2019 at some point? It’s been broken for ages and no-one seems to care.