“If you can't explain it to a six year old, you don't
understand it yourself.”
― Albert Einstein

Thursday, February 14, 2013

.NET Developer Interview Question: Managed vs Unmanaged Code

Question: What is the difference between managed or unmanaged code? What would you prefer?

Answer: According to Wikipeida, Managed code is a term coined by Microsoft to identify computer program source code, that requires and will only execute under the management of a Common Language Runtime Virtual Machine (resulting in bytecode).

On the other hand, Unmanaged code is what you use to make before Visual Studio .NET 2002 was released. Visual Basic 6, Visual C++ 6, heck, even that 15-year old C compiler. In this scenario, the application builds into assembly code (unlike managed code which builds into bytecode).

About the preference, it depends on the use cases. When developing for hardware or OS, you have to write unmanaged code or at least, inside a component of your managed code project (when for example working with unmanaged code libraries such as Win32). Whereas when developing a Business Information System, you should avoid the unmanaged code parts as much as possible.Remark 1: There is a managed version of C++ which was introduced by Microsoft. So you should not get into the trick that C++ applications are categorized unmanaged ones.Remark 2: Some people use "Native Code" as a synonym for "Unmanaged Code". But you should consider that Native Code might also refer to the output of JIT compiler, the machine code that actually runs inside the environment.