-4

What are some reasons for using unsafe code?

  1. Direct access to the GC and better performance
  2. The ability to throw types not-inherited from Exception, better performance, and the ability to write inline IL
  3. The ability to write inline IL and better performance
  4. Better performance and more control over unmanaged interop
  5. More control over unmanaged interop, better performance, and the ability to write inline IL
wonderful world
  • 10,969
  • 20
  • 97
  • 194

1 Answers1

2

Unsafe code is required if you need to use pointers which should be rare in .net.

From the docs here:

The use of pointers is rarely required in C#, but there are some situations that require them. As examples, using an unsafe context to allow pointers is warranted by the following cases:

  • Dealing with existing structures on disk
  • Advanced COM or Platform Invoke scenarios that involve structures with pointers in them
  • Performance-critical code
bryanmac
  • 38,941
  • 11
  • 91
  • 99