concept unsafe code in category .net

appears as: unsafe code
C# in Depth

This is an excerpt from Manning's book C# in Depth.

One important thing to note is that this section is only applicable to safe code, which means all C# code that isn’t explicitly within an unsafe context. As you might judge from the name, code within an unsafe context can do various things that safe code can’t, and that may violate some aspects of normal type safety, although the type system is still safe in many other ways. Most developers are unlikely ever to need to write unsafe code, and the characteristics of the type system are far simpler to describe and understand when only safe code is considered.

  • Fixed-size buffers— More control over how structs handle arrays in unsafe code.
  • 7.6. Fixed-size buffers in unsafe code

    When calling into native code with P/Invoke, it’s not unusual to find yourself dealing with a structure that’s defined to have a buffer of a particular length within it. Prior to C# 2, such structures were difficult to handle directly, even with unsafe code. Now you can declare a buffer of the right size to be embedded directly with the rest of the data for the structure.