Permalink
Fetching contributors…
Cannot retrieve contributors at this time
76 lines (65 sloc) 2.52 KB
title ms.date ms.prod ms.technology ms.topic f1_keywords dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author translation.priority.ht
unsafe (C# Reference)
2015-07-20
.net
devlang-csharp
article
unsafe_CSharpKeyword
unsafe
CSharp
unsafe keyword [C#]
7e818009-1c6e-4b9e-b769-3728a01586a0
19
BillWagner
wiwagn
cs-cz
de-de
es-es
fr-fr
it-it
ja-jp
ko-kr
pl-pl
pt-br
ru-ru
tr-tr
zh-cn
zh-tw

unsafe (C# Reference)

The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers. For more information, see Unsafe Code and Pointers.

You can use the unsafe modifier in the declaration of a type or a member. The entire textual extent of the type or member is therefore considered an unsafe context. For example, the following is a method declared with the unsafe modifier:

      unsafe static void FastCopy(byte[] src, byte[] dst, int count)  
{  
    // Unsafe context: can use pointers here.  
}  

The scope of the unsafe context extends from the parameter list to the end of the method, so pointers can also be used in the parameter list:

unsafe static void FastCopy ( byte* ps, byte* pd, int count ) {...}  

You can also use an unsafe block to enable the use of an unsafe code inside this block. For example:

      unsafe  
{  
    // Unsafe context: can use pointers here.  
}  

To compile unsafe code, you must specify the /unsafe compiler option. Unsafe code is not verifiable by the common language runtime.

Example

[!code-cscsrefKeywordsModifiers#22]

C# Language Specification

[!INCLUDECSharplangspec]

See Also

C# Reference
C# Programming Guide
C# Keywords
fixed Statement
Unsafe Code and Pointers
Fixed Size Buffers