Visual Studio constructor creation shortcuts

Visual Studio has shortcuts for almost everything. Getting to know even some of these can save you a lot of time. This post shows examples of some constructor creation shortcuts…

parameterless constructor
type ‘ctor’ and tab twice for parameterless constructor

select which parameters explicitly (shown below)
place cursor at the class level declaration
open ‘Quick Actions’ via the button on the left or Ctrl+. shortcut
Click ‘Generate Constructor’ and select which properties to include

single property constructor
place cursor at the particular property
open ‘Quick Actions’ via the button on the left or Ctrl+. shortcut
Click ‘Generate Constructor ‘Classname(prop type’)’

multiple contiguous properties
select two or more properties
open ‘Quick Actions’ via the button on the left or Ctrl+. shortcut
Click ‘Generate Constructor ‘Classname(prop type, prop type…’)’

Visual Studio constructor shortcuts

Close the console window when debugging stops in Visual Studio

Visual Studio by default will keep a console app open after debugging. A lot of the time after debugging console apps we want to look at the output so this is ideal.

If your console app just does setup work like inserting test DB records or you otherwise just want it to close immediately CHECK the ‘Automatically close the console when debugging stops’ checkbox on the Tools -> Options -> Debugging menu GUI.

Close console after debugging finishes

Of course the funny thing is that Visual Studio tells you how to do this in the console window whenever the checkbox is not checked but sometimes we just zone out and don’t look properly.

 

How to edit multiple lines at once in Visual Studio

In Visual Studio to edit multiple lines which are vertically aligned at once press Alt and select the aligned block and then just start typing as in example 1 below. This approach overwrites the selected block.

If you want to edit multiple lines in non aligned blocks you need to press Ctrl+Alt and explicitly click where you want each cursor to be and then just start typing as in example 2 below. With this approach you only add new code not overwrite existing code.

Don’t forget about the Delete button if you want to quickly remove snippets/blocks rather than add/amend them.

Editing multiple lines at once in Visual Studio

Enabling view refresh after .CSHTML changes for new projects in Visual Studio

Since .NET Core 3.0 your views in MVC won’t refresh after you change your .CSHTML markup. You can enable this for existing projects by installing Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation and setting AddRazorRuntimeCompilation() in your Startup.cs -> ConfigureServices method…

… and …

for new projects you can check the ‘Enable Razor runtime compilation’ checkbox for 3.1 projects onwards when creating the projects.

Enable view refresh for new projects in Visual Studio

Creating .NET 5 projects in Visual Studio WITHOUT editing the project file after creation

If you want to create a .NET 5 console or web app you’ll notice by default Visual Studio will create it as 3.1 without giving you the option to select .NET 5 even if its available. This is because .NET Core 3.1 is the latest LTS (long term support) version.

After project creation you can of course go into project settings and change it to .NET 5…

… BUT …

To have Visual Studio allow you select the framework version upfront when creating the project check out the highlighted option below in the Environment -> Preview Features settings screen.

Select .NET 5 when creating a new project in Visual Studio