I hear/read about it sometimes when talking about .NET, for example "managed code" and "unmanaged code" but I have no idea what they are and what are their differences. What are their difference, by definition? What are the consequences of using either of them? Does this distinction exist in .NET/Windows only?
3 Answers
Managed Code
Managed code is what Visual Basic .NET and C# compilers create. It runs on the CLR (Common Language Runtime), which, among other things, offers services like garbage collection, run-time type checking, and reference checking. So, think of it as, "My code is managed by the CLR."
Visual Basic and C# can only produce managed code, so, if you're writing an application in one of those languages you are writing an application managed by the CLR. If you are writing an application in Visual C++ .NET you can produce managed code if you like, but it's optional.
Unmanaged Code
Unmanaged code compiles straight to machine code. So, by that definition all code compiled by traditional C/C++ compilers is 'unmanaged code'. Also, since it compiles to machine code and not an intermediate language it is non-portable.
No free memory management or anything else the CLR provides.
Since you cannot create unmanaged code with Visual Basic or C#, in Visual Studio all unmanaged code is written in C/C++.
Mixing the two
Since Visual C++ can be compiled to either managed or unmanaged code it is possible to mix the two in the same application. This blurs the line between the two and complicates the definition, but it's worth mentioning just so you know that you can still have memory leaks if, for example, you're using a third party library with some badly written unmanaged code.
Here's an example I found by googling:
#using <mscorlib.dll>
using namespace System;
#include "stdio.h"
void ManagedFunction()
{
printf("Hello, I'm managed in this section\n");
}
#pragma unmanaged
UnmanagedFunction()
{
printf("Hello, I am unmanaged through the wonder of IJW!\n");
ManagedFunction();
}
#pragma managed
int main()
{
UnmanagedFunction();
return 0;
}

- 3,749
- 2
- 21
- 24
-
11"Since you cannot create unmanaged code with Visual Basic or C#, all unmanaged code is written in C/C++."? You know, there are other languages than C, C++, C#, and VB. I use Delphi to write unmanaged code. Also, one rather visible difference between managed (.NET) and unmanaged (Win32) code, is that the former can use all the .NET functions, whereas the latter use the native Windows API. – Andreas Rejbrand Aug 25 '10 at 10:37
-
22The terms 'managed' and 'unmanaged' were invented to distinguish machine code from IR. So, they only really have meaning in the context of **.NET**. The linux kernel also compiles to unmanaged code, but that's not really relevant to the discussion, is it? – kurige Aug 25 '10 at 11:09
-
6This is getting off-topic, but my point is that you don't have the *option* of writing **managed** code in Delphi, so stating that you write **unmanaged** code in Delphi is highly redundant. In any case, I've added "in visual studio" to the offending sentence. Also, thanks for the heads up on the typo. Somehow I missed it even with a second read-through. – kurige Aug 25 '10 at 15:41
-
Just to be a pedand, it's possible to write unmanaged code in C# ([known as "unsafe" code](http://msdn.microsoft.com/en-us/library/chfa2zb8.aspx)) – Basic Sep 23 '13 at 19:26
-
@Basic Nope. 'unsafe' C# allows you to use pointers. The code still runs on the CLR, along with all of the benefits. It's still 'managed'. – kurige Jun 24 '14 at 20:46
-
2@JacksonTale Java is not in the context of .NET thus the concept of it being managed or unmanaged isn't applicable. See http://stackoverflow.com/questions/1326071/is-java-a-compiled-or-an-interpreted-programming-language for an explanation of how Java compiles. – Evan Frisch Mar 23 '15 at 16:59
This is more general than .NET and Windows. Managed is an environment where you have automatic memory management, garbage collection, type safety, ... unmanaged is everything else. So for example .NET is a managed environment and C/C++ is unmanaged.

- 1,023,142
- 271
- 3,287
- 2,928
-
By this definition, JavaScript would be unmanaged code as well, correct? – Hampton Terry Oct 14 '14 at 22:24
-
1@HamptonTerry Javascript has automatic memory management, garbage collection and type safety... So no. Javascript is managed. – Sancarn Jun 20 '17 at 22:08
Managed code is a differentiation coined by Microsoft to identify computer program code that requires and will only execute under the "management" of a Common Language Runtime virtual machine (resulting in Bytecode).

- 56,863
- 21
- 114
- 161
-
1Yes. To add to this answer, managed code is the code you write normally in .NET (in C#, for instance), using the .NET functions. Native Windows applications using the native Windows API (written in any language, C perhaps), on the other hand, are "unmanaged". – Andreas Rejbrand Aug 25 '10 at 08:00