Managed Code vs Unmanaged Code

As a .NET developer, you may hear or see the terms “managed code” and “unmanaged code” from time to time. You may even be asked about them in job interviews. They are terms that can often be overlooked by new .NET developers that are just starting to learn. I think it’s important to at least have a high-level idea of the two terms so I whipped up this blog post to try and illustrate.

Common Language Runtime (CLR)

A runtime environment is typically a virtual machine that contains the necessary hardware and software components to provide machine abstraction and support executing a particular type of program’s code in real time. The Common Language Runtime (CLR) in .NET does just that. It manages the execution of .NET programs.

Managed Code

Managed code is code that’s compiled with a compiler that targets a specific runtime. As mentioned above, for .NET that would the CLR. Managed .NET code is able to benefit from services provided by the CLR such as memory and thread management, type safety, exception handling, garbage collection, and security, which all make life easier for the developer. Any language that is written in .NET is managed code that targets the CLR.

Unmanaged Code

As the name suggests, unmanaged code is the opposite. It’s code that’s not managed by a runtime. Unmanaged code is typically executed directly by the operating system instead of a runtime environment like the CLR. The compiler always compiles source code to the native code that’s specific to the CPU and architecture of the computer it’s being compiled on. This puts the burden of memory and thread management, type safety, exception handling, garbage collection, and security all on the developer. Most unmanaged programs are written in C/C++.

comments powered by Disqus