Author : Rahul
Last Modified : 12-Sep-2020
Complexity : Intermediate

Managed and Unmanaged code in c#

Managed Code

Managed code is the code that is developed using the .NET framework. Languages such as C# or VB.NET are managed. Managed code is directly executed by the Common Language Runtime (CLR or Runtime).

The managed runtime environment provides different types of services like garbage collection, type checking, exception handling, etc. to code automatically without the knowledge of the programmer. It also provides object creation, memory allocation, and object disposal etc to the code. The code which is written in Java, C#, VB.Net, etc are managed code.

In the case of .NET Framework, the compiler always compiles the manages code in the intermediate language(MSIL) and then create an executable. When the programmer runs the executable, the Just In Time Compiler of CLR compiles the intermediate language code to the native code which is specific to the underlying architecture.


  1. It implements the garbage collection automatically.
  2. It provides runtime type checking.
  3. It checks whether the reference, point to the valid object or not.
  4. It provides security to the application.


  1. You can not allocate memory directly.
  2. you can not get the low-level access of the CPU architecture.

Unmanaged Code

The code that is developed outside of the .NET framework is known as unmanaged code. Languages such as C, C++ or VB are unmanaged. The object creation, execution, and disposal of unmanaged code are directly managed by the programmers. If programmers write bad code, it may lead to memory leaks and unwanted resource allocations.

The .NET Framework provides a mechanism for unmanaged code to be used in managed code and vice versa. The process is done with the help of wrapper classes.

Unmanaged code is directly executed by the operating system. When we want to execute the same code for the different architecture we need to recompile that code again according to that architecture. It always compiles to the native code that is specific to the architecture.

In case of unmanaged code, the memory allocation, type safety, security, etc are managed by the developer. An error related to memory like buffer overflow, memory leak, pointer override, etc may occur in the code. The code which is written in VB 6.0, C, C++, etc is unmanaged code.


  1. It provides low-level access to the programmer.
  2. It also provides direct access to the hardware.
  3. programmer can skip some parameters and restriction that are used by the managed code framework.


  1. It does not provide security to the application.
  2. Exceptions handling are done by the programmer.
  3. It does not implement garbage collection automatically.

Difference between Managed and Unmanaged code:

Managed Code Unmanaged Code
It is executed by the CLR. It is executed by the Operating System.
Error related to memory buffer overflow does not occur. Error related to memory buffer overflow may occur.
It provides runtime services like Garbage Collection, exception handling, etc. It does not provide runtime services like Garbage Collection, exception handling, etc.
An application written in .NET Framework is secure. Application is not secure.
The source code is compiled into MSIL and then native language. The source code is directly compiled into the native language.