C# Tutorial Part 3 - Managed and Unmanaged Code

Before reading this article, please go through the following article:

CLI & CLR :  Part 2

Managed and Unmanaged code

Managed Code

Any code that is written to work with the services of the runtime execution environment is called Managed code. Services of the runtime execution environment include exception handling, garbage collection, type checking and so on. Managed code is always executed by the runtime execution environment and not the operating system directly. Using the services provided by the runtime execution environment, managed code will never worry about memory allocations, type safety and so on.

Managed code execution process


When the managed code is compiled it gets compiled into intermediate language code (Microsoft Intermediate Language (MSIL)) and this intermediate language is again compiled into native code (which is understood by the CPU) by the JIT compiler.

Manage Code

Unmanaged code

Any code that is developed outside the .Net framework is known as unmanaged code. Code written in C, C++, VB (VB versions prior to VB .NET) and so on are all considered as examples of unmanaged code. Unmanaged code is compiled into native code which is architecture or machine specific which means it always depends on the platform on which it is compiled and cannot be executed on any other platforms. No services are provided to unmanaged code by the runtime execution environment and the developer should handle the memory allocation, type safety and so on. 

Unmanaged code execution process

When the unmanaged code is compiled, it gets compiled into native code directly that is processed by the CPU.

Unmanaged code execution

When do we use Managed / Unmanaged code

To make it simple, you would use managed code if you want to avail the .NET framework services and would use unmanaged code if you need any service not provided by the .NET framework.

Previous article :  CLI & CLR :  Part 2

Up Next
    Ebook Download
    View all
    FileInfo in C#
    Read by 9.7k people
    Download Now!
    Learn
    View all