New features in .NET 8

Munib ButtSenior .NET/Azure Developer/Analyst Consultant
CERTIFIED EXPERT
Senior technical systems architect/ analyst and developer/mentor with over 30 years’ experience (20+ years in Canada)
Published:
Today, we will look into some new features that are expected in .NET 8. .NET 8 will be the next version of the .NET framework after .NET 7. It will be an LTS (Long-term support) release.
How to use .NET 8
The .NET 8 SDK can be downloaded from the below URL:
 
 https://dotnet.microsoft.com/en-us/download/dotnet/8.0

 Kindly note that to use .NET 8 in Visual Studio, we would need to have Visual Studio 2022 (v17.6 Preview 1) or a later version. 

 Creating the project and code 

I will be creating this application using Visual Studio 2022 Community Version 17.6.0 Preview 2.0 edition. 


Please follow the below steps:
Create a C# console application.





Now, add the below two classes and code:


// FrozenDictionaryType.cs
 
using System.Collections.Frozen;
 
namespace DotNet8Features
{
    internal class FrozenDictionaryType
    {
        private static readonly FrozenDictionary<string, string> staticData =
             LoadStaticData().ToFrozenDictionary();
 
        private static Dictionary<string,string> LoadStaticData()
        {
            return new Dictionary<string, string>() { { "Key1","Value1" }, { "Key2", "Value2" }, { "Key3", "Value3" } };
 
        }
 
        internal string GetValue(string key) 
        {
            if (staticData.TryGetValue(key, out string? keyValue))
            {
                return keyValue;
            }
 
            return string.Empty;
        }
 
    }
}
 
 
// JsonInterfaceSerializer.cs
 
namespace DotNet8Features
{
    public interface ICar
    {
        public string? Brand { get; set; }
    }
 
    public interface ISedan : ICar
    {
        public string? Model { get; set; }
    }
 
    public class CarDetails : ISedan
    {
        public string? Brand { get; set; }
        public string? Model { get; set; }
    }
}
 
 
// Program.cs
 
using DotNet8Features;
using System.Text.Json;
 
 
// Feature 1: Interface serialization
 
ISedan detail = new CarDetails { Brand = "Honda", Model="Civic" };
var serializedValue = JsonSerializer.Serialize(detail); 
Console.WriteLine(serializedValue);
 
 
// Feature 2: Frozen dictionaries
 
FrozenDictionaryType frozenDictionaryType = new();
Console.WriteLine($"The value returned from the frozen dictionary is {frozenDictionaryType.GetValue("Key2")}");
 


Now, when we run our application, we see the below:
 


Here, we have used two features of .NET 8.

The first feature is the ability to serialize properties from interface hierarchies. We see this feature implemented in the JsonInterfaceSerializer class. Here we created a base interface called ICar and a sub interface called ISedan. We then created a class instance implementing ISedan and assigned values to both properties in the immediate interface and its parent interface. Both these values were serialized using the “System.Text.Json” package.


The second feature is the new performance focused type “FrozenDictionary”. This type does not allow changes to keys and values in the dictionary after it has been created. This is for improved read operations.
 
The code is available at the below location:
 
https://github.com/munibrbutt/articles-code/tree/main/Some%20.NET%208%20features


Summary
 

In today’s article, we looked at two new features that are expected in .NET 8. As .NET 8 is still in preview, we cannot be 100% sure these will be included in the same form in the final version. However, both of these features seem very useful to boost productivity and performance of our future applications.

0
565 Views
Munib ButtSenior .NET/Azure Developer/Analyst Consultant
CERTIFIED EXPERT
Senior technical systems architect/ analyst and developer/mentor with over 30 years’ experience (20+ years in Canada)

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.