O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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* ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required