Category Archives: C#

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 direct call. We… Read More »

Building a voxel engine part 2 of 2

This is part 2 of 2. These posts are meant to give a basic introduction to voxel engines. In later posts I will dig into optimization techniques and benchmarking. In part 1 I touched on the basics of polygon mesh and voxel graphics. I also touched how a voxel world can be represented in memory to bypass some… Read More »

Building a voxel engine part 1 of 2

This is part 1 of 2. These posts are meant to give a basic introduction to voxel engines. In later posts I will dig into optimization techniques and benchmarking. So its that wonderful time of the year again where I play with something of my own choosing, uninterrupted for a few days. I left my work at work,… 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 much about project… Read More »

sizeof() vs Marshal.SizeOf()

To get the size of a data type in .Net you can use sizeof()  or Marshal.SizeOf() . I’ll briefly explain the difference between the two. sizeof() 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 data types you… Read More »

Cost of method wrapper

Introduction What happens if a method is just a wrapper for another method? Is the extra jump optimized away by compiler? Does it take much time? I thought I’d look into this and measure a bit. With the different compilers, Jits and runtimes I thought it would be fun to see what happens. I’ll use a == operator… Read More »

Speeding up Unitys Vector in lists/dictionaries

Introduction With this post I am digging into some performance improvements for Unity 3D’s Vector2, Vector3, Vector4 and Quaternion. The short version is that they really need IEquatable<T>  and can benefit from a better GetHashCode() . I’m demonstrating this with example of how it severely decreased performance in my project. Adding IEquatable<T> has no side-effects, if is actually best… Read More »

Simple app.config appSettings deserializer (with tree structure)

There are many ways to read configuraiton files in .Net. Using ConfigurationManager you can easily read for example appSettings section. But usually I want this to be in managed code. The solution often suggested is to create separate sections with a varying degree of manual work, field mapping and xml readers. Many of the solutions also suggest that… Read More »

Norske fødselsnummer i C# / Norwegian national ID numbers in C#

Code to verify Norwegian national id number (fødselsnummer / personnummer) written in C#. Obeys 2013 standards as described in this Wikipedia article. Please let me know how if you find bugs / improvements. :)

(PS: This code is easy to test in LINQPad. Select C# Program, paste it in and hit run.)

C# Regex for verifying e-mails

Regex for matching e-mail in C#. Came across which has a horrible example, so ended up making my own loosely based on that. It leaves much to be desired, but will cover most use cases well enough.