| |

The type or namespace name InterceptsLocationAttribute could not be found

If you’re just getting started with interceptors, you might be getting one of the following errors: Error CS0246 The type or namespace name ‘InterceptsLocationAttribute’ could not be found (are you missing a using directive or an assembly reference?) Error CS0246 The type or namespace name ‘InterceptsLocation’ could not be found (are you missing a using…

| |

NET 8 – Mark JsonSerializerOptions as readonly

Starting with .NET 8 you can mark JsonSerializerOptions instances as read-only, preventing further changes to the instance. To freeze the instance, simply call MakeReadOnly on the options instance. Let’s take an example: Furthermore, you can check if an instance was frozen or not by checking the IsReadOnly property. Attempting to modify a JsonSerializerOptions instance after…

| |

NET 8 – Include non-public members in JSON serialization

Starting with .NET 8 you can include non public properties in the serialization when using System.Text.Json. To do so, simply decorate the non-public property with the JsonIncludeAttribute attribute. The attribute works with any non-public modifier, such as private, protected or internal. Let’s look at an example: As expected, this will output the following:

|

dotnet workload clean

Note: this command is only available starting with .NET 8. This command cleans up workload packs that might be left over after an .NET SDK or Visual Studio update. It can be useful when encountering issues while managing workloads. dotnet workload clean will clean up orphaned packs resulted from uninstalling .NET SDKs. The command will…

|

NET 8 – Deserialize into read-only properties

Starting with .NET 8 you can deserialize into properties which do not have a set accessor. You can opt-in for this behavior using JsonSerializerOptions, or on a per-type basis using the JsonObjectCreationHandling attribute. Using JsonObjectCreationHandling attribute You can annotate your type with the System.Text.Json.Serialization.JsonObjectCreationHandling attribute, passing your option as a parameter. Using JsonSerializerOptions You can…

NET 8 – Handle missing members during JSON deserialization
|

NET 8 – Handle missing members during JSON deserialization

By default, if you have additional properties in a JSON payload you are trying to deserialize, they are simply ignored. But what if you wanted the deserialization to fail and throw an exception when there are extra properties in the JSON? That is possible starting with .NET 8. There are several in which you can…

.NET 8 – JSON serialize property names using snake case and kebab case
|

.NET 8 – JSON serialize property names using snake case and kebab case

.NET 8 introduces several new naming policies that can be used with the System.Text.Json serializer. To name them: Let’s look at the serialized output for each of them. For this, we’re going to use a Car class with the following definition: And we are going to serialize the following object instance: Lower snake case (snake_case)…

.NET 8 performance: Dictionary vs. FrozenDictionary
|

.NET 8 performance: Dictionary vs. FrozenDictionary

With .NET 8 we are introduced to a new dictionary type which improves the performance of read operations. The catch: you are not allowed to make any changes to the keys and values once the collection is created. This type is particularly useful for collections that are populated on first use and then persisted for…

|

What’s new in .NET 8

.NET 8 was released on November 14, 2023 as an LTS (Long Term Support) version, meaning it will continue to receive support, updates, and bug fixes for at least three years from its release date. As usual, .NET 8 brings support for a new version of the C# languange, namely C# 12. Check out our dedicated page…