CRV2 FrameworkSpecIssuesASPNetUnsafeCode

C# Unsafe Code

Even though C# has a strong memory management infrastructure, there will be times when is necessary to direct access memory:

Dealing with existing structures on disk

Advanced COM or Platform Invoke scenarios that involve structures with pointers in them

Performance-critical code (Microsoft, 2009)

Microsoft strongly discourages the use of the unsafe code when this is not necessary. It is clear that even when using unsafe code might improve performance in the program, the risks might overcome the benefits. Definitely, this is no area for inexperienced programmers.

Unsafe is used by declaring the “unsafe” keyword in the program code. For example: