Unsafe reference arithmetic in .NET

Taking a closer look at the CompilerServices Unsafe approach to unsafe reference arithmetic over fixed statement in .NET. .NET has a garbage collector (GC) that automates memory management. This is incompatible with working with direct memory pointers, since the GC needs to track object usage and move objects around when required. You are of course … Read more

Faster C# array access

In .Net memory access is safe by default. This means that every time you access an array item the index is checked against the length of the array. In this post I explore how you can speed up plain old array access 18%-34% by convincing the compiler it can skip some checks. I compile the … Read more

Demystifying async/await (with memes)

If you have used async/await, but feel some details are still a bit fuzzy then this may be for you. I thought I’d hop down into the rabbit hole and give some examples of how async/await actually works. Hopefully this will provide some understanding and get you further on your journey to become an expert. … Read more

Extension support for .Net Core application

There are various reasons why one would want to support plugins or extensions for an application. It allows third party to extend application, one can isolate development of parts of the application, and pick what to load at runtime. Whatever the motivation, this means loading .dll’s after the application has started, finding interfaces of interest … Read more

.Net Core Console Application IoC

The default setup for .Net Core MVC application is to use IoC. You get a Startup.cs file created that contains boilerplate code for IoC. If you want to use the builtin IoC with .Net Core you have to write it yourself. Luckily the HostBuilder makes this easy. HostBuilder is provided by the Generic Host introduced … Read more

Benchmarking dynamic method invocation in .Net

Benchmarking method execution through interface, base class, virtual, override, dynamic, reflection and expression trees. In .Net there are many ways to execute a method. The fastest being the straight forward call to a static method. But how does its speed compare to other methods? There are countless reasons why we sometimes can’t just make a … Read more

Setting up new .Net projects

Intro I have been a developer for somewhere around 30 years now. In my time I have created a few thousand projects of varying sizes, and for every new iteration I try to improve slightly on my previous. I also see a lot of code from others, and most what I see don’t seem care … Read more

sizeof() vs Marshal.SizeOf()

To get the size of a data type in .Net you can use   or  . I’ll briefly explain the difference between the two. sizeof()   (MSDN) can only be used on data types that have a known size at compile-time. It is compiled as a constant. If you attempt to use it on invalid … Read more