Overview
Officially .NET 6 will be released in November 2021, but for the time being the Microsoft community has released 6 previews of .Net 6, which type of features are included in .NET 6.
All the features are not supported in the stable version of visual studio 2019, so here I have attached a demo that I performed in the visual studio 2022 preview version.
As per the preview released by Microsoft, we found some quite useful features that we are going to discuss here in this blog.
PriorityQueue
- We all are aware of Collections and Collections are mainly of 2 types: Generic and Non-generic.
In .NET 6 inside Generic Collections it means under System.Collection.Generic namespace we have a new collection is PriorityQueue <TElement, TPriority>. - Priority Queue is one type of Queue but with priority so for Insert or Remove element inside priority queue that time operations are the same.
- For Inserting any value into PriorityQueue we will use Enqueue() operation and Removing any value we will use Dequeue() operation.
Now, we think this new collection is similar to Generic Queue so what are the differences between Queue<T> and PriorityQueue.
Mainly we have 2 differences between them.
- Enqueue: When we perform an Enqueue operation in PriorityQueue at that time we must specify the priority of each and every element.
- Dequeue: When we perform Dequeue operation in PriorityQueue at that time, first of all, removes that element that has lowest priority in the queue.
Code:
var pq = new PriorityQueue<string, int>(); pq.Enqueue("Alice", 3); pq.Enqueue("Bob", 1); pq.Enqueue("Can", 2); pq.Enqueue("Dean", 3); for (int i = 0; i < 4; i++) { Console.WriteLine("Remove Element is : {0}", pq.Dequeue()); }
Output:
Remove Element is : Bob Remove Element is : Can Remove Element is : Dean Remove Element is : Alice
Hot reload with the Vs Debugger
- Hot reload is a very useful process but sometimes when we change a little bit of code in our application and after applying that code we need to reload our application. That time Hot reload is a time-consuming process.
- In .NET 6 we have a new button Apply Code Changes button that enables it when our application is running in the Browser, now we can easily apply our code when our application is running without restarting or reloading the application.
DistinctBy
- We know that DistinctBy is selector function which return those objects which generate unique results now in .NET 6 we will use in C# code.
Code :
var result = Enumerable.Range(1, 20).DistinctBy(x => x % 4); Console.Write("Output is :"); foreach (var item in result) { Console.Write(" " + item); }
Output:
Output is : 1 2 3 4
MaxBy/MinBy
- MaxBy and MinBy use for find maximum and minimum value depends on our condition.
- When our condition is based on Int data type that time easily find maximum and minimum value but when condition based on String data type that time for MinBy first sort all records in alphabetical ascending order and same for MaxBy first sort sort all records in alphabetical descending order and return value.
Code :
var result = Enumerable.Range(1, 20).DistinctBy(x => x % 4); var people = new (string name, int age)[] { ("John", 20), ("Rose", 30), ("Mark", 40) }; var max = people.MaxBy(person => person.age); var min= people.MinBy(person => person.age); Console.WriteLine("output for MinBy :" + min.name); Console.WriteLine("output for MaxBy :" + max.name);
Output:
Output for MinBy :John Output for MaxBy :Mark
UnionBy
- UnionBy method is used to combine the result-set of two or more SELECT statements and The UnionBy method selects only distinct values by default.
- As above DistinctBy only returns those types of results which generate unique records so when we use UnionBy that time duplicate records are automatically removed, which are duplicate records it decided by our conditions.
Code :
var groupA = new (string Name, int Age)[] { ("John", 20), ("Rose", 30), ("Mark", 40) }; var groupB = new (string Name, int Age)[] { ("Jack", 30), ("Harry", 30), ("Joy", 33) }; var result = groupA.UnionBy(groupB, person => person.Age); foreach (var item in result) { Console.WriteLine("Name : {0} , Age : {1}", item.Name, item.Age); }
Output:
Name : John , Age : 20 Name : Rose , Age : 30 Name : Mark , Age : 40 Name : Joy , Age : 33
DateOnly and TimeOnly Struct
- We are aware about DateTime, In .NET 6 DateOnly and TimeOnly are now used for displaying only Date part portion or only Time part portion.
- We will use DateOnly whenever we want to display the only Date like leave days, birthdays and working days etc.
- TimeOnly is used whenever we want to display the only Time like office time, alarm clocks and break time etc.
- In .NET 6 both are provided in struct form.
Compression
- Now, in .Net 6 Compression is supported in single file, to enable this feature we need to set the EnableCompressionInSingleFile property to true.
- After enabling this feature files are automatically compressed at a runtime and provide huge space saving.
Default font in windows form
- This feature for windows form now we are able to set our default font as we want in our windows form by Add a single line of code in our program.cs file
Application.SetDefaultFont(new Font(new FontFamily("Consolas"), 40f));
- We need to specify font style and font width in our program class as above and after that we easily change our default font in our windows form application.
Code :
static class Program { [STAThread] static void Main() { Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.SetDefaultFont(new Font(new FontFamily("Consolas"), 40f)); Application.Run(new Form1()); } }
Output:
Click here for the reference of above mentioned features https://github.com/yudiz-dotnet/dotnet-6-preview-features
Read: How our development process increased the sales of Dangee Dums
Conclusion
.NET 6 Preview has truly arrived with a number of features, affecting low-level library features with source generators and analyzers. Hope you have enjoyed and are very curious about releasing this feature of .NET 6. Thanks for reading.