NET 8 – Serialization of Half, Int128, and UInt128
System.Text.Json brings out of the box support for additional types: Let’s look at an example:
C# (pronounced see sharp, like the musical note ) is a general-purpose, multi-paradigm programming language encompassing strong typing, lexically scoped, declarative, imperative, functional, generic, object-oriented, and component-oriented programming disciplines. It was introduced in 2000 by Microsoft as part of its .NET initiative, and later approved as an international standard by Ecma (ECMA-334) and ISO (ISO/IEC 23270:2018).
System.Text.Json brings out of the box support for additional types: Let’s look at an example:
Starting with .NET 8, both Memory<byte[]> and ReadOnlyMemory<byte[]> are serialized as Base64 strings. Let’s look at a quick example: In contrast, Memory<int> and the likes will continue to be serialized as JSON arrays.
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:
Inline arrays enable you to create an array of fixed size in a struct type. Such a struct, with an inline buffer, should provide performance comparable to an unsafe fixed size buffer. Inline arrays are mosly to be used by the runtime team and some library authors to improve performance in certain scenarios. You likely…
C# 12 introduces a new simplified syntax for creating arrays. It looks like this: It’s important to note that the array type needs to be specified explicitly, so you cannot use var for declaring the variable. Similarly, if you wanted to create a Span<int>, you can do: Multi-dimensional arrays The advantages of this terse syntax…
You can use the WaitForExit method to wait for the process to complete. Your code will wait synchronously for the process to finish, then it will resume execution. Let’s look at an example: The code above will start a new cmd.exe process, and execute the timeout 5 command. The process.WaitForExit() call will force your program…
The using alias directive has been relaxed in C# 12 to allow aliasing any sort of type, not just named types. This means that you can now alias tuples, pointers, array types, generic types, etc. So instead of using the full structural form of a tuple, you can now alias it with a short descriptive…
The With…End With statement in VB allows you to execute a series of statements that epeatedly refer to a single object. Thus the statements can use a simplified syntax for accesing members of the object. For example: Is there a C# syntax equivalent? No. There is not. The closest thing to it would be the…
Starting from C# 12, it is possible to define a primary constructor within classes and structs. The parameters are placed in parentheses right after the type name. The parameters of a primary constructor have a broad scope. They can be utilized to initialize properties or fields, serve as variables in methods or local functions, and…
C# is a modern, general-purpose, object-oriented programming language developed by Microsoft. It is widely used for Windows desktop applications, games (especially using the Unity engine), and web development through the ASP.NET framework. C# is considered to be beginner-friendly and is a great language for new programmers. Below we explore some of the reasons why C#…
As technology continues to evolve, so does the C# programming language. With the release of C# 12 on the horizon, developers can look forward to a host of exciting new features and enhancements that promise to streamline their coding experience and open up new possibilities in software development. If you haven’t yet switched to C#…
While trying out C# 12 features, it’s possible you might come across errors similar to these: Feature is not available in C# 11.0. Please use language version 12.0 or later. or Error CS8652: The feature ‘<feature name>’ is currently in Preview and unsupported. To use Preview features, use the ‘preview’ language version. There are two…