Using The Hot Reload Feature In Visual Studio 2019

Introduction

 
In today’s article, we will look at a feature just introduced with Visual Studio 2019. This is the hot reload feature. In the past, if we made any code change in the backend code, we would need to restart the application to apply this change. This has now changed with the new hot reload feature. We can change our backend code on the fly while running the application, apply these changes, and see the results of the new code all while the application is running. Let us take a look at a simple implementation of this new feature.

Creating the VS 2019 application

 
For now, this feature is available with the Visual Studio 2019 Preview Version 16.11.0 Preview 1.0 version as below,
 
Using The Hot Reload Feature In Visual Studio 2019 
 
This feature should also be part of the upcoming Visual Studio 2022 version.
 
Let us create a Windows forms application. I am only creating this application as it is a simple way to demonstrate this new feature. The new hot reload feature can also be used with other types of applications like WPF, ASP.NET, etc.
 
Using The Hot Reload Feature In Visual Studio 2019
 
Using The Hot Reload Feature In Visual Studio 2019
 
Using The Hot Reload Feature In Visual Studio 2019
 
Using The Hot Reload Feature In Visual Studio 2019
 
Using The Hot Reload Feature In Visual Studio 2019 
 
Here, we will add a button and label it to the form. When we click on the button, we simply change the text as below,
  1. using System.Threading.Tasks;  
  2. using System.Windows.Forms;  
  3.   
  4. namespace WinFormsAppHotReload  
  5. {  
  6.     public partial class Form1 : Form  
  7.     {  
  8.         public Form1()  
  9.         {  
  10.             InitializeComponent();  
  11.         }  
  12.   
  13.         private void button_Click(object sender, EventArgs e)  
  14.         {  
  15.             label.Text = "The value is set to one";  
  16.         }  
  17.     }  
  18. }  
Now, we run the application and click on the button,
 
Using The Hot Reload Feature In Visual Studio 2019 
 
Now suppose we want to change the message. Previously we would need to stop the application, change the code, and run it again. However, with the new hot reload feature, we can do this while the application is running.
 
While the application is running, open the code file and make the change as below,
 
Using The Hot Reload Feature In Visual Studio 2019 
 
Then click as below,
 
Using The Hot Reload Feature In Visual Studio 2019 
 
Now, if you click the button again, you get the updated code message as below,
 
Using The Hot Reload Feature In Visual Studio 2019

Summary

 
In this article, we looked at the new hot reload feature introduced in Visual Studio 2019. This will certainly make the development process much quicker and robust. Microsoft states as this is a new feature it might still contain some bugs. However, we can expect it to be very solid by the time Visual Studio 2022 is released.
 
Happy coding!