Managed And Unmanaged Code

Managed Code

  • Managed Code means the code which is designed and developed under the .NET Framework called Managed Code.
  • The code is designed under .NET and supports many programming languages such as C#, VB.NET, and F#.
  • The code is monitored and follows the protocols of Common Langauge Runtime (CLR).
  • All the things like Object Creation, Memory Allocation, and disposal of the object after its use are the things that are managed under the CLR.
  • Also any code which is written in .NET Framework is Managed Code.

Advantages of Managed Code

  • The CLR will take care of Managed Code like Memory Allocation, Security, and Memory Disposal.
  • CLR will automatically check all the things if there are unwanted objects present in the memory then it will check that using a garbage collector and handle the scenario.

Disadvantages of Managed Code

The major disadvantage of the Managed Code is we are not able to use memory as per our needs and not able to interfere with the CPU Memory Architecture.

Unmanaged Code

  • The Code which is written using the .NET Framework is called Unmanaged Code.
  • The Code which is not executed under the CLR like C and C++ is also known as Unmanaged Code.
  • All the things like Memory Allocation, Object Creation, and Disposal of that Object is need to taken care of by the developer, there are chances of uncertainty like memory leakage and unwanted memory allocation.
  • But .NET Framework provides some techniques using that Unmanaged Code as a Managed Code and vice-versa by using the Wrapper Classes which are present under the .NET Framework.

Advantages of Unmanaged Code

  • You can run the Unmanaged Code under any environment and platform.
  • The code is a little bit faster than managed code.
  • The developer will be able to pass some parameters without following the protocols under the Common Language Runtime.

Disadvantages of Unmanaged Code

  • Less Secure than Managed Code.
  • It may face memory leakage issues and performance issues.
  • It will not support default exception handling, the developer needs to take care of that things.
  • Hard to manage type casting.
  • It will not support Automatic Memory Management-related things which are provided by CLR under the .NET Framework.

So, this is all about Managed and Unmanaged Code.

I hope you now understand the few things related to Managed and Unmanaged Code.