Quick Actions in Visual Studio 2017

Quick Actions as part of Visual Studio 2017, provide suggestions to re-factor code or fix code style .

I wanted to explore Quick Actions further and wanted to see how it can be helpful. The below Microsoft documentation provides additional details on settings which needs to be updated in Visual Studio to customize Quick Actions.

https://docs.microsoft.com/en-us/visualstudio/ide/code-styles-and-quick-actions

I started to create a console application using .Net Core project template. Visual Studio creates the required project files with below code in Program.cs

using System;

namespace TestCoreConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}

On placing cursor over the method, quick actions icon will be shown as yellow bulb. On clicking the bulb, it provides two options to re-factor the code.

  1. Option is provided to change method signature.
  2. Use expression body for methods

QuickAction1

On clicking the second option the code gets updated as below.

static void Main(string[] args) => Console.WriteLine("Hello World!");

This style can be used when the method is having only one line of code.

On placing the cursor again over the converted line, we get quick action tip to consider changing the previous converted method to use block body for methods.

QuickAction2

So at this point if we convert back to use block body for methods, quick action tip will suggest to consider expression body for methods. This suggestion appears like a cycle.

I wanted to tweak the text editor settings as shown below.

QuickAction3

On performing the above , I could notice three dots … mentioning suggestion. This suggestion would appear only when there is a single line in the method.

QuickAction4

This auto suggestion disappears when we start typing new line in the main method.

QuickAction5

This way one can tweak the text editor settings according to your coding standards. This is my blog post for C# advent calendar and thanks to Matt Groves(@mgroves) for asking me to sign up for this.

You can follow the C# advent calendar for other posts here.

 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s