Home
Map
unsafe KeywordUse unsafe code blocks and the unsafe keyword to manipulate memory directly.
C#
This page was last reviewed on Sep 11, 2023.
Unsafe. In C#, unsafe things access memory directly. The "unsafe" keyword introduces a lot of complexity to programs. It is best avoided.
The unsafe context allows unsafe memory use. It is used to implement algorithms in an efficient way. This is a modifier. Often we must also enable "unsafe" code in Visual Studio.
An example. This program adds the unsafe modifier and fixed keyword to use char* pointers on a string's buffer. Fixed asserts that the memory location should not be moved in memory.
Tip The "\0" character is used by the .NET Framework to terminate a string. We need to know this only for unsafe code.
char
using System; class Program { unsafe static void Main() { fixed (char* value = "sam") { char* ptr = value; while (*ptr != '\0') { Console.WriteLine(*ptr); ++ptr; } } } }
s a m
Unsafe block. We can also use an unsafe block inside a method. This has an equivalent result. This spaces code out and emphasizes the unsafe modifier more.
Note If you try to compile these programs, you will get this error: "Unsafe code may only appear if compiling with /unsafe".
And To solve this in Visual Studio, go to Project Properties, then Build, and check "Allow unsafe code".
using System; class Program { static void Main() { unsafe { fixed (char* value = "sam") { char* ptr = value; while (*ptr != '\0') { Console.WriteLine(*ptr); ++ptr; } } } } }
s a m
Fixed. We use fixed buffers inside an unsafe context. With a fixed buffer, you can write and read raw memory without any of the managed overhead.
And We use the fixed statement to create an unmovable memory block. This allows pointer access.
fixed
Stackalloc. This operator replaces the regular allocators. This memory is freed when the enclosing function returns. Stackalloc can be useful for external DLL calls.
Warning Stackalloc is not usually a good optimization in managed code. It introduces some overhead.
stackalloc
Some examples. These examples may be helpful in understanding. They do not focus on unsafe features in particular, but rather instances where unsafe code could be used.
GetHashCode
Some comments. The best thing to do with unsafe code is to avoid it. Unsafe code makes programs slower. It makes them harder to understand.
However This applies unless you know exactly what you are doing and are willing to put a lot of effort into your solution.
Summary. Unsafe code is rarely needed. Its most important use is inside the .NET Framework. There it optimizes critical methods such as GetHashCode and string copy routines.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 11, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.