sizeof

An additional feature that can be used only in unsafe code deals with objects and sizes. Mono provides the keyword sizeof, which does nothing other than return the size of an object in bytes. As you can see in the next example, sizeof can be used easily:

using System;

public class Demo
{
        static void Main()
        {
                double x = 3241.15;
                unsafe
                {
                        byte *y = (byte*) &x;

                        for (int i = 0; i < sizeof(double); ++i)
                        {
                                Console.Write("{0} - {1,4}",
                                        i, (uint) (*y++));
                                Console.WriteLine();
                        }
                }
        }
}

Sometimes the entire function isn't based on unsafe code. In many cases, it can be useful to mark only a few pieces as unsafe. In our example, a double is defined in the safe code. In the unsafe block, we create a pointer and assign a value to it. After that, we ...

Get Mono Kick Start now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.