What is new in .NET 6?
Latesh Sarode
Software Architect @ Globant , Ex Sr Engineering Manager @ICICI Group | Designing Cost Optimized Solutions | Business Continuity | Security
.NET 6 is the latest version of .NET that was released in Nov 2021. Not only is?.NET 6?a much improved version of the framework compared to its predecessors, but it also introduces some of the coolest features we’ve seen in some of the most popular platforms and languages. This article talks about the latest updates and new features introduced in in .NET 6.0.
1.??Single file deployment –?
We get an option to produce a single file, in the below screenshot it shows the publish setting of visual studio which shows the single file setting ,?
If you see the real size of the exe released content its approx. 65 MB –?
However , if you check the same in the publish folder the single file would have bit more size because its loading all required dependencies –?
2.??Flatten sequence with LINQ select many?
If we wish to print all colors we need two foreach loops in the traditional way?
SelectMany minimizes the need of having two different foreach loops as seen below -?
3.??DateOnly extension methods?
In .Net 6 we have two new extension methods called as TimeOnly and DateOnly
You can build your own extension methods around this class as below –?
And then you can call these methods from your calling application as below –?
4.??Humanizer nuget package
It helps to bring your output to more readable format , once you install the same from manage nuget packages you get to call some extension methods as seen below –?
If you check above output on the left hand side its printing the timespan in more readable format.
5.??Return ReadOnlyCollection with AsReadOnly method –?
Here a trap you can fall into while you are working with the properties that returns IEnumerable<T> and you want to restrict users of your call to add any items
?In the below example we have IEnumerable of string as FlowersSold and we have a public constructor which adds an item into it ,?
The issue is my calling application can anytime consume and may keep adding items in it and that I may not want –
????I was able to add an item in the list as below –?
There might be several ways to solve this issue , in .net 6 we can simply achieve this by adding below code –?
And if we try to compile the same code it will work fine , however on runtime we will get an error as below –?
6.??Build Linux and Windows path strings?
One of the nice this that comes with .net core is you can build your application irrespective of any operating system, how do you get file path information in your application if you are running your application in Linux or Windows
?
To do this , install wsl , and go the home directory where our code is residing
If we do ./TipsConsole here on linux terminal its showing me the path and contents of Readme.txt file
below red line is Linux specific path?
Same thing we can do it in PowerShell , below red line is windows specific path?
Ho do I got these files ?
When we try to publish from visual studio you can create your own profiles for different target operating systems as shown below?
To get file contents from Linux as well as Windows below is the code –?
7.??Create fake test data with Bogus?
Another way of generating data while doing unit testing is the Bogus library.
领英推荐
Although , there are number of libraries available in the market – Bogus generates the fake data.
Consider the below windows based application which contains the below 4 columns ,?
Below is POCO class for the same –?
The first one is the basic library called Bogus , we need to install it?
I have list created for customer type as below –?
This is how you can create a faker and generate the fake objects using Bogus ,
8.??Version endpoints for web API??
In .net we have minimal APIs by which we can maintain the api version
While we create .net core api project , make sure we uncheck below check box (Use Controllers)?-
If we check csproj file , we will see the Version number as below –?
We will also have lot of boilerplate code added along with below –
Below is the code to use Minimal API , code to be added in program.cs (from line# 39 to 46)
Using the above URL you will get back the string , which contains versioninfo , time when it was last updated and time stamp
When we run the application we will see swagger page with the below two APIs?
If we run /api-version api which is a GET method ,
It will return us following ,?
Its really useful to quickly get the version number while doing unit testing?
9.??Determine shared key values with the LINQ?toLookup method.
If you worked with GroupBy in any your code in LINQ ,toLookup does the similar operation it means you specify a key and then it returns into a sequence based on the key.
There is some benefits of using ToLookup ,
If you check the below code , the ToLookup is of type ILookup , In line no 24 below is declared a type of ILookup as lookByCardFamily and then we are doing a lookup by key.
The main benefit here over line no 27 where we have used GroupBy is it doesn’t throw any exception even when we do not have any items with the given key , where as Groupby does.
10.??Create a Zip archive file –
One of the ask was “What nuget package do you use to make zip files ?”
Typically we do not need any nuget library for this as we have lot of compression packages available an it’s a simple process.
Consider the below example ,?
CardImages is a folder that contains images . then I have some code to Zip and UnZip the files and the location in on my desktop.
?We use System.IO.ZipFile and it comes along with lot of readymade functions as shown below –
This is how we can use above methods –?
************.NET 6 Is The Fastest .NET So Far*****************
.NET 6 is supported by Visual Studio, Visual Studio Code, and CLI tools.
.NET 6 is the fastest version of .NET.
// Happy Coding !!