13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 25 Unsafe code25. Unsafe codeAn implementation that does not support unsafe code is required to diagnose any usage of the keywordunsafe.This remainder of this clause is conditionally normative.[Note: The core <strong>C#</strong> language, as defined in the preceding chapters, differs notably from C and C++ in itsomission of pointers as a data type. Instead, <strong>C#</strong> provides references and the ability to create objects that aremanaged by a garbage collector. This design, coupled with other features, makes <strong>C#</strong> a much safer languagethan C or C++. In the core <strong>C#</strong> language it is simply not possible to have an uninitialized variable, a“dangling” pointer, or an expression that indexes an array beyond its bounds. Whole categories of bugs thatroutinely plague C and C++ programs are thus eliminated.While practically every pointer type construct in C or C++ has a reference type counterpart in <strong>C#</strong>,nonetheless, there are situations where access to pointer types becomes a necessity. For example, interfacingwith the underlying operating system, accessing a memory-mapped device, or implementing a time-criticalalgorithm may not be possible or practical without access to pointers. To address this need, <strong>C#</strong> provides theability to write unsafe code.In unsafe code it is possible to declare and operate on pointers, to perform conversions between pointers andintegral types, to take the address of variables, and so forth. In a sense, writing unsafe code is much likewriting C code within a <strong>C#</strong> program.Unsafe code is in fact a “safe” feature from the perspective of both developers and users. Unsafe code mustbe clearly marked with the modifier unsafe, so developers can’t possibly use unsafe features accidentally,and the execution engine works to ensure that unsafe code cannot be executed in an untrusted environment.end note]25.1 Unsafe contextsThe unsafe features of <strong>C#</strong> are available only in unsafe contexts. An unsafe context is introduced by includingan unsafe modifier in the declaration of a type or member, or by employing an unsafe-statement:• A declaration of a class, struct, interface, or delegate may include an unsafe modifier, in which casethe entire textual extent of that type declaration (including the body of the class, struct, or interface) isconsidered an unsafe context.• A declaration of a field, method, property, event, indexer, operator, instance constructor, destructor, orstatic constructor may include an unsafe modifier, in which case, the entire textual extent of thatmember declaration is considered an unsafe context.• An unsafe-statement enables the use of an unsafe context within a block. The entire textual extent of theassociated block is considered an unsafe context.The associated grammar extensions are shown below. For brevity, ellipses (...) are used to representproductions that appear in preceding chapters.class-modifier:...unsafestruct-modifier:...unsafe317

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!