Safe and Unsafe Code (C# vs Java)

A particularly interesting feature of C# is its support for non-type-safe code. Normally, the common language runtime (CLR) takes on the responsibility for overseeing the behavior of Microsoft intermediate language (MSIL) code, and prevents any questionable operations. However, there are times when you wish to directly access low-level functionality such as Win32 API calls, and you are permitted to do this, as long as you take responsibility for ensuring such code operates correctly. Such code must be placed inside unsafe blocks in our source code.

The unsafe Keyword

C# code that makes low-level API calls, uses pointer arithmetic, or carries out some other unsavory operation, has to be placed inside blocks marked with the unsafe keyword. Any of the following can be marked as unsafe:

  • An entire method.

  • A code block in braces.

  • An individual statement.

The following example demonstrates the use of unsafe in all three of the above situations:

class TestUnsafe
{
    unsafe static void PointyMethod()
    {
        int i=10;

        int *p = &i;
        System.Console.WriteLine("*p = " + *p);
        System.Console.WriteLine("Address of p = {0:X2}\n", (int)p);
    }

    static void StillPointy()
    {
        int i=10;

        unsafe
        {
            int *p = &i;
            System.Console.WriteLine("*p = " + *p);
            System.Console.WriteLine("Address of p = {0:X2}\n", (int)p);
        }
    }

    static void Main()
    {
        PointyMethod();
        StillPointy();
    }
}

In this code, the entire PointyMethod() method is marked unsafe because the method declares and uses pointers. The StillPointy() method marks a block of code as unsafe as this block once again uses pointers.

The fixed Keyword

In safe code, the garbage collector is quite free to move an object during its lifetime in its mission to organize and condense free resources. However, if your code uses pointers, this behavior could easily cause unexpected results, so you can instruct the garbage collector not to move certain objects using the fixed statement.

The following code shows the fixed keyword being used to ensure that an array is not moved by the system during the execution of a block of code in the PointyMethod() method. Note that fixed is only used within unsafe code:

class TestFixed
{
    public static void PointyMethod(char[] array)
    {
        unsafe
        {
            fixed (char *p = array)
            {
                for (int i=0; i<array.Length; i+)
                {
                    System.Console.Write(*(p+i));
                }
            }
        }
    }

    static void Main()
    {
        char[] array = { 'H', 'e', 'l', 'l', 'o' };
        PointyMethod(array);
    }
}

See Also

Tasks

Unsafe Code Sample

Concepts

C# Programming Guide

Reference

Unsafe Code and Pointers (C# Programming Guide)

Other Resources

The C# Programming Language for Java Developers