← All topics

64 articles in the notebook

C#.

Unsafe reference arithmetic in .NET

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.

Why C# multidimensional arrays are slow

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.

Faster C# array access

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.

Demystifying async/await (with memes)

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.

ASP.NET Core MVC

The opening recipe in my ASP.NET Core series: setting up a clean MVC project as the foundation the rest of the recipes build on.

Extension support for .Net Core application

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.

.Net Core Console Application IoC

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.

Setting up new .Net projects

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() vs Marshal.SizeOf()

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.

Cost of method wrapper

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.

Template replacement engine

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# Regex for verifying e-mails

The famous emailregex.com example is horrible, so I made my own C# email regex — loosely based on it, compiled, and good enough for most use cases.

ASP.Net 5 Areas

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.

Better text console for C#

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.

Alternative directory layout for MVC

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.

.Net GUI thread crossing

"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.

Early alpha release: Kinect C# Starter Kit

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.

Kinect

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.

Compression algorithm

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.

WPF MVVM Thread annoyance

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.

MVVM (and my IRC client)

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.

C# / .Net WinForms GUI Thread Invoke solution

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.

Embedding YouTube in WPF WebBrowser control

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.

Help on IRC client

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.

Nimbus.IRC

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.

.Net tools to use: log4net

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: Nini

.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.

Pure C# support

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.

LSL to C#

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.

Book: CLR via C#, Second Edition

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.

.Net CLR Runtime

Can LSL bytecode map onto .NET CLR? Yes — and here is the biggest Hello World I have ever written, generated at run-time with ILGenerator and OpCodes.