Programming has been with us for almost a century now but the cycle surprisingly has largely been the same:

There have been a lot of incremental improvements to improve this cycle over the decades.

For example in for .NET languages (C#, VB.NET and F#), if you are using the SDK, you compile your programs like this:

dotnet build

You then run them like this:

dotnet run

It was quickly realized that it is tedious having to run two commands given that to run a program you probably want to build it first. And so dotnet run first builds and then proceeds to run the program.

Another improvement is you can run a program but also instruct the SDK to rebuild it if it detects a change in the code.

Instead of running dotnet run you run this:

dotnet watch

This is accomplished using the dotnet watch tool.

If the source code changes dotnet watch will rebuild the code and then re-launch the program.

.NET 6 has a further improvement - Hot Reload.

Hot Reload allows developers to change the source code of the application and have those changes reflect in real-time by modifying the running application.

This is different from just re-compiling and re-running. Here the effect is of the application essentially being re-written in memory while it is still loaded.

Take this simple program which fires an event every 5 seconds:

using (var timer = new PeriodicTimer(TimeSpan.FromSeconds(5)))
{
    while (await timer.WaitForNextTickAsync())
    {
        WriteToConsole(DateTime.Now);
    }
}
void WriteToConsole(DateTime time)
{
    Console.WriteLine($"Still firing at {time}");
}

If we run it it will print the following:

Firing at 9 Nov 2021 09:41:13
Firing at 9 Nov 2021 09:41:18
Firing at 9 Nov 2021 09:41:23
Firing at 9 Nov 2021 09:41:28
Firing at 9 Nov 2021 09:41:33

With the program still running, make a change to the source code:

Before:

After:

Immediately the code is saved, the output changes:

  • A - the change in the source code is detected
  • B - the Hot Reload updates the changes and applies them to the running application.

If you look closely at the timestamps you will notice the event still fires after exactly 5 seconds even factoring in the changes.

Needless to say, there are limits to the sort of changes that can me made in real-time - these are outlined here

Thoughts

This is a brilliant aid to developers that will make the edit - build - run cycle less frictional; improving productivity.

TLDR

Hot Reload allows running programs to have their source edited and applied immediately while they are still running.

There was some controversy when Microsoft attempted to pull this feature from Release Candidate 2 and restrict it to Visual Studio. The community was infuriated and the outrage led to it’s reinstatement.

Happy hacking!

This is Day 25 of the 30 Days Of .NET 6 where every day I will attempt to explain one new / improved thing in the upcoming release of .NET 6.