Understanding the CLR (Common Language Runtime) is the prerequisite to understand managed and unmanaged code
The. Net Framework is made up of two separate and interrelated parts: The CLR and the class library, the CLR is the service it provides to us, and the class library is the function it implements.
. NET most of the features----garbage collection, versioning, thread management, etc., all using the services provided by the CLR
When you compile the source code for the. NET framework, the resulting target code is not a machine instruction that the CPU can recognize, but rather a new language called "Microsoft Intermediate Language (MSIL, or code abbreviated as IL)". The CLR provides a real-time compiler,
Used to compile IL code into native machine code. In this way, the CLR can make the code portable because. NET application's source code must be compiled into IL code that can run on any platform that provides CLR services. From the perspective of the CLR,
All languages are equal, as long as there is a compiler that can generate IL code, which ensures interoperability of all languages.
Managed code-Managed Code
Code that is executed by the common language runtime (CLR) environment, not directly by the operating system. Managed code applications can obtain common language runtime services, such as automatic garbage collection, runtime type checking, and security support.
These services help provide platform-and language-independent, unified managed-code application behavior.
Unmanaged code-Unmanaged
Code that is executed directly by the operating system outside of the common language runtime environment. Unmanaged code must provide its own garbage collection, type checking, security support, and other services, unlike managed code, which obtains these services from the common language runtime.
C # 's understanding of managed and unmanaged code