A closer look at CompilerServices.Unsafe as the modern alternative to the fixed statement: doing reference arithmetic on GC-managed memory without pinning, what "unsafe" really means, and what the CLR does and doesn't promise you.
int[,] looks like the natural choice and performs like the wrong one. Digging into IL and JIT output to show what actually happens when you access single-dimensional, multidimensional and jagged arrays — and why the rectangular one loses.
Every array access in .NET pays for a bounds check — unless you convince the compiler it can skip it. I read the JIT'ed x64 assembly and benchmark tricks that make plain array access 18-34% faster.
Async/await looks simple and is widely misunderstood — I have seen entire conference talks get it wrong. A trip down the rabbit hole with examples (and memes) of what the compiler actually does: no, async does not create a thread.
Plugins for a .NET Core app boil down to loading .dlls at runtime, finding the interesting interfaces and registering them in IoC. Since IoC already gives you loose coupling, dynamic loading is a small step — shown here.
MVC gets IoC boilerplate generated for free; console apps don't. With HostBuilder from the Generic Host it's easy to wire up the same dependency injection, config and logging in a console application — here is the boilerplate.
Interface, base class, virtual, override, dynamic, reflection or expression trees — .NET gives you many ways to call a method, and their costs differ wildly. Full benchmark with numbers for each.
Part 2 of the voxel engine introduction: turning chunk data into renderable polygon meshes in Unity, one Mesh per chunk, and only drawing the faces that can actually be seen.
My Christmas vacation project: a basic introduction to voxel engines à la Minecraft. Part 1 covers what voxels are, polygon meshes, and how to represent a voxel world in memory without hitting limits.
Unity happily lets you write C# but not structure it. Four ways to share code between proper Visual Studio projects and Unity — so your libraries stay testable and your Unity project stops being a big ball of mud.
After ~30 years and a few thousand projects, this is how I set up new .NET solutions: naming, project structure, .NET Standard vs Core choices — and the reasoning behind each habit. Not a book on architecture, just a clean, consistent setup.
sizeof() compiles to a constant; Marshal.SizeOf() measures the marshaled struct at runtime. What each one really returns, what it costs, and when to use which — with IL to prove it.
What does a method that just calls another method actually cost? Using the == operator vs IEquatable.Equals as the guinea pig, I benchmark wrapper calls across compilers, JITs and runtimes — and read the disassembly to see why.
Unity's Vector2/3/4 and Quaternion lack IEquatable and have a poor GetHashCode, which quietly murders performance the moment you put them in lists or dictionaries. Measurements, explanation, and the fix I reported to Unity.
Reading appSettings shouldn't require custom sections, XML readers or per-access string conversion. A small base class that maps keys like Config.SmtpClient.Host to a typed tree, converted once at startup.
Regex.Replace with a match evaluator plus a case-insensitive dictionary makes a tidy single-pass template engine — faster and more flexible than chaining string.Replace calls.
C# code to validate Norwegian national ID numbers (fødselsnummer), following the 2013 standards — including D-, H- and FH-numbers, gender and birthdate extraction. Available as the Tedd.Fodselsnummer NuGet package.
Bulk-importing users through Identity Framework 3 kept failing with disposed-object and modified-collection errors. The culprit: async void. Change to async Task and await properly — code included.
Visual Studio 2015 has no GUI support for Areas in ASP.Net 5 yet. The workaround is simple: create the folder structure manually and add an area route in Startup.cs — shown here.
Game loops check timers constantly, so how cheap is reading time? Environment.TickCount: 7ms for a million reads. DateTime.Now.Ticks: 1392ms. The numbers, and when each is the right choice.
Console.MoveBufferArea turned out to be dead slow under heavy logging output. So I went to the Win32 console API and built a faster text console for C# — with source and benchmarks.
Forgetting to swap config files at deployment can be disastrous. This approach keeps all environment configs in one file, selects the right one automatically, and uses ACLs so developers can't read production secrets.
The default MVC folder layout scatters one feature across Controllers, Views and Scripts. As the project grows, grouping by feature instead keeps related files together — here is how to bend ASP.Net MVC to that layout.
I rented a Battlefield Heroes server and discovered the admin tooling wanted an app running 24/7 on my PC — so I wrote a .NET library for the Rcon protocol instead. Protocol details and source inside.
There are plenty of C# web servers already, but I wanted to play with TPL and async/await — so here is a minimal HTTP server built around them. Downloadable code included.
How well does Dictionary<int,int> actually perform? I measured speed and memory with random data and random access on real hardware — with numbers, graphs and a few surprises about what hides behind O(1).
ShadowCopy means web apps run their .dlls from a temp directory, so "the directory of my assembly" is a trap. Static helpers that resolve the real application path for web, console, WinForms and WPF alike.
A .dll shared between web and desktop apps can't assume where app.config or web.config lives. A small cached method that resolves the correct config file wherever the assembly is running, with Nini example.
For DSP debugging I want to draw FFT data fast: kernel32-allocated memory, unsafe pointers, and InteropBitmap so WPF renders straight from the buffer. Vacation-time code, constantly improving.
"Cross-thread operation not valid" explained from the ground up: what the GUI thread actually does, why only it may touch your controls, and how to cross over correctly in WinForms and WPF.
A starter platform for Kinect hacking in C#: MVVM-bound module engine, sample modules doing silly image processing, easy to extend with your own. Buy a Kinect, install drivers, open this project and start playing.
Bought a Kinect, hooked it up to the PC, fought the drivers for a couple of hours — and then got raw image data access working in C#. Sample code for fast access to the Kinect's byte stream.
Every now and then I get a bright idea for a compression algorithm. This one took 3.5 hours to compress the 5 characters "Tedd!" into 7 characters, after 143 billion iterations. Not every bright idea survives contact with math.
Playing Age of Conan wasn't enough tinkering, so I wrote an app that screenshots the game window, analyzes the pixels and reacts — totally useless, great fun, and a nice excuse to play with image analysis in C#. Source included.
The VS2010 conversion wizard is defect: it tries to rewrite your code when only the solution and project files actually need changing. Convert, then keep only the new .sln and .csproj — step-by-step guide inside.
I hoped WPF had left WinForms' GUI-thread invoking behind. Then BindingList and DependencyProperty both demanded GUI thread invocation from my ViewModel — which isn't supposed to know the GUI exists. ARGH.
A personal rant on whether to use MVVM, written while heavily refactoring my IRC client. Comparing MVVM with my current module architecture — clearly MVVM is not optimal, but there might be parts worth borrowing.
Tired of Invoke-ing the GUI thread from every background thread, I ran Reflector on WinForms' Form, generated a partial class with a Perl script, and got a Form where every method safely invokes the GUI thread automatically. A quick hack, but it works.
Every ASP.Net app I make emails me its unhandled exceptions — including a MiniDump of the process that you can load into Visual Studio as a paused project and inspect stack, locals and all. Here is the code.
Embedding YouTube in a WPF app via the WebBrowser control works — once you know about Mark of the Web, line endings, and that Flash forces your app to run as x86. All three lessons learned the hard way, sample code included.
My C#/WPF IRC client is functional but needs GUI work, so I'm looking for beta testers and developers of all skill levels. Everything is modular, so you can work on single features without touching unrelated code.
Ten years of writing IRC clients on and off, and this time I'm fairly happy with what's under the hood. Nimbus.IRC is module-based with a Common library and a GUI shell, and alpha releases are coming.
A deep dive into user space threading for .NET: why OpenSim needs to pause, serialize and resume thousands of running scripts, and the whole arsenal — from Mono.Cecil IL rewriting to fibers — I have thrown at the problem.
First in a small series on .NET tools worth using: log4net, Apache's free logging framework. One line at the top of each class, a standard XML section in web.config, and you have proper leveled logging.
.NET tools to use, part two: Nini, an uncommonly powerful configuration library. Reads web.config, .ini and .xml config with type conversion and default values in a couple of lines.
Helped a guy on IRC with a method that reads a file or stream line by line, runs a custom replacement callback on each line, and writes the result out — without eating memory on large files. BSD-licensed, take it.
The ScriptEngine now mixes LSL and C# in the same script — and if a script starts with //C#, the LSL preprocessor is skipped entirely for 100% pure C# support. A few naming rules apply for events.
Instead of compiling LSL directly, I first translate it to C# — simpler for other OpenSim developers to contribute to, and the same translator can double as a learning tool for people moving from LSL to C#. Written in less than a day with regex and a couple of passes.
The book everyone calls the bible of the CLR arrived a week early. 648 pages between me and a much deeper understanding of what .NET actually does with my code — fuel for the LSO project.