reinterpret_cast in C#

You can achieve this but this is a relatively bad idea. Raw memory access like this is not type-safe and can only be done under a full trust security environment. You should never do this in a properly designed managed application. If your data is masquerading under two different forms, perhaps you actually have two separate data sets?

In any case, here is a quick and simple code snippet to accomplish what you asked:

byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int byteCount = bytes.Length;

unsafe
{
    // By using the fixed keyword, we fix the array in a static memory location.
    // Otherwise, the garbage collector might move it while we are still using it!
    fixed (byte* bytePointer = bytes)
    {
        short* shortPointer = (short*)bytePointer;

        for (int index = 0; index < byteCount / 2; index++)
        {
            Console.WriteLine("Short {0}: {1}", index, shortPointer[index]);
        }
    }
}

There are four good answers to this question. Each has different downsides. Of course, beware of endianness and realize that all of these answers are holes in the type system, just not particularly treacherous holes. In short, don't do this a lot, and only when you really need to.

  1. Sander's answer. Use unsafe code to reinterpret pointers. This is the fastest solution, but it uses unsafe code. Not always an option.

  2. Leonidas' answer. Use StructLayout and FieldOffset(0) to turn a struct into a union. The downsides to this are that some (rare) environments don't support StructLayout (eg Flash builds in Unity3D) and that StructLayout cannot be used with generics.

  3. ljs' answer. Use BitConverter methods. This has the disadvantage that most of the methods allocate memory, which isn't great in low-level code. Also, there isn't a full suite of these methods, so you can't really use it generically.

  4. Buffer.BlockCopy two arrays of different types. The only downside is that you need two buffers, which is perfect when converting arrays, but a pain when casting a single value. Just beware that length is specified in bytes, not elements. Buffer.ByteLength helps. Also, it only works on primitives, like ints, floats and bools, not structs or enums.

But you can do some neat stuff with it.

public static class Cast {

    private static class ThreadLocalType<T> {

        [ThreadStatic]
        private static T[] buffer;

        public static T[] Buffer
        {
            get
            {
                if (buffer == null) {
                    buffer = new T[1];
                }
                return buffer;
            }
        }
    }

    public static TTarget Reinterpret<TTarget, TSource>(TSource source)
    {
        TSource[] sourceBuffer = ThreadLocalType<TSource>.Buffer;
        TTarget[] targetBuffer = ThreadLocalType<TTarget>.Buffer;

        int sourceSize = Buffer.ByteLength(sourceBuffer);
        int destSize = Buffer.ByteLength(targetBuffer);
        if (sourceSize != destSize) {
            throw new ArgumentException("Cannot convert " + typeof(TSource).FullName + " to " + typeof(TTarget).FullName + ". Data types are of different sizes.");
        }

        sourceBuffer[0] = source;
        Buffer.BlockCopy(sourceBuffer, 0, targetBuffer, 0, sourceSize);
        return targetBuffer[0];
    }
}

class Program {
    static void Main(string[] args)
    {
        Console.WriteLine("Float: " + Cast.Reinterpret<int, float>(100));
        Console.ReadKey();
    }
}

c# supports this so long as you are willing to use unsafe code but only on structs.

for example : (The framework provides this for you but you could extend this to int <-> uint conversion

public unsafe long DoubleToLongBits(double d)
{
    return *((long*) (void*) &d);
}

Since the arrays are reference types and hold their own metadata about their type you cannot reinterpret them without overwriting the metadata header on the instance as well (an operation likely to fail).

You can howveer take a foo* from a foo[] and cast that to a bar* (via the technique above) and use that to iterate over the array. Doing this will require you pin the original array for the lifetime of the reinterpreted pointer's use.

Tags:

C#

Arrays

Casting