In November we are receiving .NET6 and Visual Studio 2022 releases, these are going to be the first stable versions available to the public.
For the time being we can have access to the preview of both products, it’s a great way to stay ahead of the technologies, even though they are not recommended for your applications that are in production, as they have some big improvements to be made. If you want to try the early-access versions, you can download them here.
On previous posts we talked about .NET MAUI , the evolution of Xamarin.Forms and the new ways of developing multi-platform applications. And those are not the only types of projects that are getting new upgrades.
Here we will talk about a new concept introduced with Net6.0 called MINIMAL API and the some differences between the new and the old version of the projects.
To start with, we have created a new project of type “”ASP.NET Core Empty”on Visual Studio 2019 and on Visual Studio 2022 (preview4)
On the left we have the Visual Studio 2019 template, working with .Net5
On the right we have the Visual Studio 2022p4 template, working with .net6.0.100-rc.1.21458.32
First, let’s compare the project structure.
We have almost the same files, but VS2022 does not have the Startup.cs
class. The Program.cs
and Startup.cs
classes got fused into one single Program.cs
file for Visual Studio 2022
Not only the amount of files has been reduced, but the amount of lines of codes.
For VS2019 we have Program.cs
with 27 lines and Startup.cs
with 34, making a total of 61 lines of code.
And on VS2022p4 we have the same code in only 6 lines. That is 10% of the code lines. Yes, you might see that the code on VS2019 is not necessary or can be simplified, but even with that the percentage will not flicker.
Another example of Minimal API
Now we will create a console aplication with each version Visual Studio, and we will compare them.
On the left we have a Console Application, using .Net5.
And on the right we have the same type of project, but using .Net6.
You will notice that the project structure it’s the same and they have the same files, but the Program.cs
class file is different. Nevertheless, you will get the same result from both projects.
With .Net6 you only have the body of the main.
If you want to learn more about this new console template, you can visit the Microsoft Documentation
How is this possible?
If we start comparing the files in each solution, we will notice that some things are missing.
All those using instructions are gone. And not only that, also is the namespace, the class declaration, and the default constructor, making the code much simpler.
You can still add new using instructions at the top of the file, create new properties, methods and more classes just like you normally would. Take into account that it will be local, so it’s not going to be available from another file.
This is achieved with one of the new features introduced with C#10.0, called global using.
Depending of the type of the project, the framework automatically adds some implicit using directives. Developers can make any namespace available globally by using the keyword global
If you are using this new version of the IDE and the framework, but you prefer to manage all the namespaces yourself as you used to, you can disable this by adding <ImplicitUsings>Disable</ImplicitUsings>
to your project file
Final thoughts
Having small and compacts projects is great for developers that start using Microsoft Frameworks, it helps them keep focus on the syntax of the language and how it works, and forget about all those using intructions and the namespaces, without losing any functionality.
As the developers learn more about the coding language, these feature will become part of your daily basis.
Before running we should learn to walk and that is achieved one step at the time.
A cleaner code is always easier to undertand and if this helps flatten the learning curve of the language, then we will have more .NET developers in the future
Please share your thoughts 😃
Curious how Xablu can help your organization ?