| | |

.NET Performance: ToList vs ToArray

.NET 9 brings significant improvements to LINQ’s ToArray method, both in terms of speed, as well as memory allocation. To achieve this, the new implementation makes use of new runtime features such as InlineArray and ArrayPool to significantly reduce memory allocations and improve speed, especially when dealing with an IEnumerable<T> of unknown length. If you’re curious about…

|

C# language version history

C# 1.0 Introduced with .NET Framework 1.0 in January 2022, C# 1.0 established the foundation of the language with basic object-oriented programming features, including classes, structs, interfaces, events, properties, and delegates. The language had the following goals: C# 2.0 Introcudec in 2005, this version brought significant enhancements such as generics for type-safe data structures, anonymous…

An example of using C# 13 params with a ReadOnlySpan.
| | |

C# 13: Use params collections with any recognized collection type

The params modifier in C# has traditionally been associated with array types, allowing methods to accept a variable number of arguments. However, starting with C# 13, you can now use params collections with a variety of collection types, broadening its applicability and making your code even more versatile. Supported collection types The params modifier now…

| | |

How to switch to C# 13

While trying out C# 13 features, it’s possible you might come across errors similar to these: Feature is not available in C# 12.0. Please use language version 13.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…

| |

MAUI: How to register handlers in a library

Whether you are developing a custom controls library or simply organizing your solution into multiple project, you will most likely end up in the situation where you want to register some view handlers and services from within a MAUI library. To start off, there’s no such thing as zero-configuration registration. MAUI uses a builder pattern…

|

How to fix: ‘Point’ does not have a predefined size, therefore sizeof can only be used in an unsafe context

The error you’re encountering is because in C#, sizeof can only be used with types that have a predefined size known at compile-time, and the Point structure is not one of those types unless you’re in an unsafe context. There are two ways you can resolve this. Use unsafe code This would allow the use…

| |

C# Access private property backing field using Unsafe Accessor

One less-known feature of the UnsafeAccessorAttribute is that it also allows you to access auto-generated backing fields of auto-properties – fields with unspeakable names. The way to access them is very similar to accesing fields, the only difference being the member name pattern, which looks like this: Let’s take the following class as an example:…

| |

C# ZIP files to Stream

.NET 8 include new CreateFromDirectory overloads which enable you to create a ZIP file without writing them to disk. This is particularly useful in situations where you don’t want to store the zipped resources, you use the Zip content only for transfer. For example: if you were to provide an API allowing multi-file download. That…

Image showing GetGenericTypeDefinition performance numbers comparing .NET 7 and .NET 8. On .NET 7, the method takes on average 13 ns to execute, while on .NET 8 it only takes 1.6 ns.
| |

.NET 8 performance: 10x faster GetGenericTypeDefinition

.NET 8 brings some neat performance for existing APIs that handle type information. One such API that has seen a significant improvement is GetGenericTypeDefinition. In my benchmarks, the .NET 8 implementation is almost 10 times faster compared to the .NET 7 version. You can run this benchmark yourself if you’d like using BenchmarkDotNet: Or if…