Unsafe Code

Unsafe code is an ominous name. What developer wants to purposely write unsafe code? In .NET, unsafe code really means potentially unsafe code, which is code or memory that exists outside the normal boundaries of the CLR. For this reason, developers should approach the use of unsafe code with caution.

In unsafe code, developers can access raw pointers. You can use pointer operators, such as * and &, with these pointers. Pointers should be avoided because they interrupt the normal operation of the GC. However, using unsafe pointers is sometimes necessary. For example, porting algorithms from C++ that rely heavily on pointers is one situation in which unsafe pointers might be beneficial.

Managed developers must call unmanaged routines sometimes. Although the breadth of the .NET Framework Class Library (FCL) is expanding, there is system behavior that still resides outside its realm. In addition, many third-party routines are not managed—particularly legacy applications. Finally, some code will never be managed for performance or other considerations. Interoperability allows managed developers to build bridges back to unmanaged code. You can call unmanaged code from a managed routine. Conversely, you can call managed code from unmanaged routines.

Unsafe code is discussed in more detail in the next chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.131.212