什么是托管代码,非托管代码(.net framework)

Managed Code
Managed Code is code that is written to target the services of the managed runtime execution environment (CLR). The Managed Code is always executed by managed Code runtime environment rather than OS directly. It refers to a method of exchanging information between the program and the runtime enviornment. The environment also provide the necessary security checks before executing piece of code. Managed code also get different services from the untime environment like
Garbage collection
Type Safety
Exception handling.
Bounds Checking
Therefore managed code does not have to worry about memory allocations, type safety. Applications written in java, c#, VB.net, etc target a runtime environment which manages the execution and the code written using these languages is known as managed code. Managed Code is always compiled into a intermediate languages (MSIL in Dot net).
These compiler used by .net framework to compile managed code compiles it into an intermediate language and generate necessary metadata and information.
In .Net framework managed code runs with in the .net framework’s CLR and get all services provided by CLR. After compile managed code an executable is created. When user run the executable the Just in time compiler of CLR compiles IL into native code specific to architecture of machine or machine code.
Managed Code also provides platform independence. As the managed code is first compiled to IL, the CLR’s JIT compiler takes care of compiling this IL into architecture specific instructions.
Unmanaged Code
Code that is directly executed by the operating System is known as an un-managed code. Applications written in VB 6.0, C++, C etc are all examples of unmanaged code. Unmanaged code typically targets the processors architecture and is always dependent on the computer architecture. If you want to run the same code on different architecture then you have to recompile the code using that particular architecture. Unmanaged code is always compiled to the native code. Unmanaged code does not get any services from the managed execution environment.
In unmanaged code the memory allocation, type safety, security needs to be taken care of by the developer. This makes unmanaged code prone to memory leaks like buffer over burns and pointer overrides etc.

Difference between Managed Code and Unmanaged Code
Code that targets the common language runtime is known as managed code. But unmanaged code is executed directly by operating System.
Managed Code is executed under the instruction of CLR. But unmanaged cannot executed under the instruction of CLR.
The code is understandable by CLR in case of managed. But in case of unmanaged code is not understand by CLR.
In managed code compilation process is done is two phase- Code to MSIL and then MSIL to machine language. But in case of unmanaged code, the code compilation is done in one phase, code to machine code.
Languages used in writing managed code are, Java, C#, VB.net, etc which uses runtime environment. But languages used in writing unmanaged code are vb 6.0, C++, C etc.
Managed code provides Garbage Collection or Automatic memory allocation and de-allocation. But unmanaged code does not provide memory allocation features.
Managed code also provides platform independence. But unmanaged code is platform dependence.
In case of managed code is compiled into MSIL (Microsoft intermediate Language). In case of unmanaged code, code is compiled into machine language.
Managed Code provides Type Safety. But unmanaged does not provide Type Safety.
Unmanaged code is more error prone then managed code.
In unmanaged code the memory allocation or other services needs to be taken care of by the developer. But developer is no need to take care of these services in managed code.

Key Features available to managed code applications includes.
Performance gained from executing all code in the CLR. Calling unmanaged code decrease performances because addition security check are required.
Ease of deployment and vast improved versioning facilities the end of “DLL Hell”.
Built-in Security by using code access security and avoiding buffer overruns.
It provides scalability features.
Provide improvement to application stability.
Provide cross-language integration.
This permits applications built with managed code to perform more safety and efficiently.

你可能感兴趣的:(.net,Security,C#,vb,VB.NET)