.Net Hot Reload

What’s Hot Reload And Why it’s Useful?

A feature that will let you see the code change result without the need to restart the application is called hot reload. It’s useful because developers don’t need to waste extra time to restart a running app every time they change a piece of code.

Is it Something New?

It already existed in the Javascript frameworks for a long time, but it’s new in C#, and not very common in statically-typed programming languages.

What Was the Controversy Around .NET 6 Hot Reload?

Before releasing the .NET 6 Microsoft announced that they are going to enable the reload functionality only for their Visual Studio 2022 and not for the for notnet watch CLI tool. This caused a huge complaint from the open-source community which convinced Microsoft to release the feature for all platforms.

How to Use Hot Reload in Visual Studio 2022

I’m going to use a console app that can calculate flat taxation and I will try to change the percentage at run time to use the hot reload feature and see how it reflects on the output without restarting the app.

int income;
while (true)
{
    Console.Write("How much was your income? $");

    if (!Int32.TryParse(Console.ReadLine(), out income))
        return;

    TaxCalculator.Calculate(income);
}

public static class TaxCalculator
{
    public static void Calculate(int income)
    {
        Console.WriteLine($"Your tax will be ${income * 0.30}");
    }
}

When you run the app you are going to see a flame icon which is representing the hot reload feature. You can check the “Hot Reload on File Save” which will implement your code change every time you save a change otherwise you have to manually click on the “Hot Reload

In the beginning, I calculated the 15% of income as the tax and then I changed it to 30% without restarting the app, and as you can see in the pictures the change affected the result.

How to Use Hot Reload in CLI tool

When it comes to CLI you just need to direct to the project directory and run the command dotnet watch and that will run your project and hot reload in case of changing code.