Unsafe Code

C#’s support for unsafe code is the language’s most significant divergence from Java. Unsafe code provides access to pointer data types and allows the programmer to work directly with the contents of memory. Unsafe features can be dangerous, are rarely needed, and should be avoided if possible. However, unsafe code is required when calling pre-.NET dynamic-link libraries (DLLs) that take pointers as arguments.

The unsafe Modifier

The unsafe features described in this section are available only within an unsafe context, declared using the unsafe modifier. The unsafe modifier is applicable to the declaration of classes, structs, interfaces, delegates, fields, methods, properties, events, indexers, operators, constructors, destructors, ...

Get C# for Java Developers 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.