What is new in .NET 6?

What is new in .NET 6?

.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 ,?

No alt text provided for this image

If you see the real size of the exe released content its approx. 65 MB –?

No alt text provided for this image

However , if you check the same in the publish folder the single file would have bit more size because its loading all required dependencies –?

No alt text provided for this image

2.??Flatten sequence with LINQ select many?

If we wish to print all colors we need two foreach loops in the traditional way?

No alt text provided for this image

SelectMany minimizes the need of having two different foreach loops as seen below -?

No alt text provided for this image

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 –?

No alt text provided for this image

And then you can call these methods from your calling application as below –?

No alt text provided for this image

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 –?

No alt text provided for this image

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 ,?

No alt text provided for this image

The issue is my calling application can anytime consume and may keep adding items in it and that I may not want –

No alt text provided for this image

????I was able to add an item in the list as below –?

No alt text provided for this image

There might be several ways to solve this issue , in .net 6 we can simply achieve this by adding below code –?

No alt text provided for this image

And if we try to compile the same code it will work fine , however on runtime we will get an error as below –?

No alt text provided for this image

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?

No alt text provided for this image

Same thing we can do it in PowerShell , below red line is windows specific path?

No alt text provided for this image

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?

No alt text provided for this image

To get file contents from Linux as well as Windows below is the code –?

No alt text provided for this image

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 ,?

No alt text provided for this image

Below is POCO class for the same –?

No alt text provided for this image

The first one is the basic library called Bogus , we need to install it?

No alt text provided for this image

I have list created for customer type as below –?

No alt text provided for this image

This is how you can create a faker and generate the fake objects using Bogus ,

No alt text provided for this image

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)?-

No alt text provided for this image

If we check csproj file , we will see the Version number as below –?

No alt text provided for this image

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)

No alt text provided for this image

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?

No alt text provided for this image

If we run /api-version api which is a GET method ,

It will return us following ,?

No alt text provided for this image

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.

No alt text provided for this image

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 ,?

No alt text provided for this image

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.

No alt text provided for this image

?We use System.IO.ZipFile and it comes along with lot of readymade functions as shown below –

No alt text provided for this image

This is how we can use above methods –?

No alt text provided for this image

************.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.


No alt text provided for this image
// Happy Coding !!        

要查看或添加评论,请登录

社区洞察

其他会员也浏览了