How To Write Very Fast C# Code

On June 15th I gave this 1-hour webinar about how to write very fast C# code. The webinar was hosted by Postsharp, creators of the Visual Studio extension of the same name.

If you are a tech leader, writing very fast C# code is a very important skill to master. It will help you and your team create scalable code that can take a beating.

In this webinar, I go through several performance optimization tricks that will help you write fast C# code. I start by showing how much throwing an exception will slow down your code. Then I take a detailed look at concatenating strings. Both the string and the StringBuilder class go head to head in a performance showdown.

My next performance measurement compares 1- and 2-dimensional array performance. Is there any difference between the two? The answer may surprise you! You may also be surprised to learn the difference in performance between a for- and a foreach-loop.

I wrap up the webinar with a very fast class factory and some highly optimized reflection code. I use some self-modifying code to build a new method from scratch, one CIL instruction at a time. If you have never seen dynamic method generation before, this is really cool stuff!

In my examples, I use some very advanced techniques including pointer operations that write directly into heap memory, and I create custom dynamic methods that I build up out of individual CIL instructions. At several points in the webinar, I’ll show you the CIL code produced by the C# compiler and point out specific code fragments that improve or impair performance.

In the video, I share a discount coupon that will give you 90% off for every single one of my training courses on Udemy. You can find my Udemy courses here:


Would you like to know more? I’ve created a series of blog posts on C# performance optimization. Each post is based on content from my courses and from actual techniques I’ve used in the field.

Take a look:

Also published on Medium.