Managed vs Unmanaged Code
1.0 Managed Code
The code is developed using the Common Language Runtime (CLR) of the .NET framework is known as Managed Code. In other words, it is the code that gets executed directly in C#. The runtime environment for managed code provides a variety of services to the programmer. Any language that is written in .NET Framework is managed code.
Managed code uses CLR which in turn looks after your applications by managing memory, handling security, allowing cross-language debugging, and so on.
1.1 How Managed Code is Executed
- The managed code is always compiled by the CLR into an intermediate language known as MSIL and after that, the executable of the code is created. The intermediate language (MSIL) in the source code is compiled by the compiler named ‘Just In Time Compiler’ when the executable is run by the programmer. Here the whole process is carried under a runtime environment, hence the runtime environment is the reason behind the working of the program.
2.0 Unmanaged Code
The Unmanaged Code is also known as Unsafe Code. Unmanaged code depends on the computer architecture as it aims for the processor architecture.
In C#, the activities like managing stacks, allocation, releasing of memory, etc. are taken care of by the CLR and hence these activities are out of the purview of the programmer. The programmer tells the compiler that the management of the code will be done by him/her when he/she uses the keyword “unsafe”. However, issues like memory leaks can happen if a programmer writes bad code.
Get Chinenye Okeke’s stories in your inbox
Join Medium for free to get updates from this writer.
2.1 How Unmanaged Code is executed
- For executing unmanaged code wrapper classes are used in C#. In C# the unmanaged code is directly executed by the operating system. Generally, the executable files of unmanaged or unsafe code are in the form of binary images which are loaded into the memory.
3.0 Managed Code vs Unmanaged Code
- Managed code is the one that is executed by the CLR of the .NET framework while unmanaged or unsafe code is executed by the operating system.
- The managed code provides security to the code while undamaged code creates security threats.
- In unsafe or unmanaged code the unsafe modifier is used to write the block of code while any other code written outside the unsafe code block is managed code.
- Memory buffer overflow problems do not occur in managed code as it is taken care of by the runtime environment but these problems occur in unmanaged code.
- Runtime services are provided in managed code while they are not provided in unmanaged code.
- Source code is first converted to an intermediate language and then to a native language in managed code while it is directly converted into the native language in unmanaged code.
- Unmanaged code provides low-level access while managed code does not provide that.
- In unsafe or unmanaged code the unsafe modifier is used to write the block of code while any other code written outside the unsafe code block is managed code.
In Conclusion, Managed Code is developed using the Common Language Runtime (CLR) of the .NET framework while Unmanaged Code depends on the computer architecture as it aims for the processor architecture.








