29.4. Using the unsafe keyword

Probably the first keyword to learn for unsafe programming is unsafe itself. This is used to demarcate a type, member, or code block which contains unsafe codes. The demarcated code region is known as the unsafe context. A compilation error occurs if unsafe codes appear outside an unsafe context.

The unsafe modifier can be applied to a class, struct, interface, delegate, field, method, property, event, indexer, operator, constructor (both instance and static constructors), or destructor. The following code fragments are examples of how the unsafe keyword is used to create an unsafe context.

10: public unsafe class UnsafeClass{
11:   // unsafe context with class
12: }

20: public class MyClass{
21:   public unsafe int* ...

Get From Java to C#: A Developer's Guide 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.