What is Managed and unmanaged Code?
Managed Code:
When a developer writes code in .Net using Visual Studio and creates executable or dynamic link libraries (exe/dll) from various applications (such as console, windows application, library project) or web forms (asp.net, asp.net web API, asp.net MVC) using .Net supported programming languages like C# or Vb.Net, all of these applications are executed under the management of the Common Language Runtime (CLR).
This implies that the CLR takes care of managing unused objects, cleaning them up through the garbage collector. Moreover, if one wishes to communicate with an application built in a different programming language, the Common Type System (CTS) ensures compatibility.
For code access security, there is Code Access Security (CAS) to check if the application has the appropriate permissions to execute the code, all of which are overseen by the CLR.
In essence, the CLR is responsible for loading and unloading all applications built using the .Net framework. Please refer to the image provided below for a visual representation.
Code written using the .Net framework and supported programming languages, which runs under the complete control of the Common Language Runtime (CLR), is referred to as "managed code." If the .Net framework or .Net runtime is not installed, these types of code will not be executed. The CLR takes on the responsibility of providing various features and functionalities of .Net to the managed code, including language interoperability, exception handling, automatic memory management, and code access security.
Unmanaged Code:
Let's consider third-party applications like Team Viewer, Microsoft Word, and Microsoft Visio. Unlike .Net applications, these programs are not developed within the .Net environment; instead, they are built using other programming languages like C/C++. When you integrate such executable files into your .Net-based application, they operate independently and are not governed by the Common Language Runtime (CLR). Instead, they function within their respective runtime environments.
Applications like Team Viewer, Microsoft Word, and Microsoft Visio, which are built in languages such as C++ or VB6, operate within their respective runtime environments and are not managed by the Common Language Runtime (CLR) used in .Net applications.
Such code, which doesn't run under the CLR environment, is referred to as "unmanaged code." Unmanaged code, unlike managed code written in .Net, does not benefit from the facilities and features provided by the CLR, such as language interoperability, exception handling, automatic memory management, and code access security.
In summary, when integrating third-party applications built with other languages into a .Net application, it's essential to be aware that they will operate independently in their own runtime environments, and the CLR will not provide any assistance or features to them.