References and Pointers, Part Two

Here’s a handy type I whipped up when I was translating some complex pointer-manipulation code from C to C#. It lets you make a safe “managed pointer” to the interior of an array. You get all the operations you can do on an unmanaged pointer: you can dereference it as an offset into an array,…


References and Pointers, Part One

Writing code in C# is really all about the programmatic manipulation of values. A value is either of a value type, like an integer or a decimal, or it’s a reference to an instance of a reference type, like a string or an exception. Values you manipulate always have a storage location that stores the…


What’s the Difference? Part Three: fixed vs. fixed

I got an email the other day that began: I have a question about fixed sized buffers in C#:  unsafe struct FixedBuffer { public fixed int buffer[100]; } Now by declaring buffer as fixed it is not movable… And my heart sank. This is one of those deeply unfortunate times when subtle choices made in the…


Iterator Blocks, Part Six: Why no unsafe code?

There are three good reasons to disallow unsafe blocks inside an iterator block. First, it is an incredibly unlikely scenario. The purpose of iterator blocks is to make it easy to write an iterator that walks over some abstract data type. This is highly likely to be fully managed code; it’s simply not a by-design scenario….