Managed and Unmanaged Code in C#

Managed and Unmanaged Code in C#

In this article, I am going to discuss the Managed and Unmanaged Code in C#. Please read our previous article where we discussed Common Language Specification (CLS) in detail. At the end of this article, you will understand what are Managed Code and Unmanaged code in C# and how they are executed in .NET Application in detail.

Understanding the Managed and Unmanaged Code in C#.NET Application:

Whenever you create any exe (i.e. console application, windows application, class library project, etc.) or web application (i.e. ASP.NET MVC, Web API, ASP.NET, etc.) in dot net using visual studio and using any dot net supported programming language such as C#, VB, etc., then these applications are run completely under the control of CLR (Common Language Runtime).

That means, if your applications having unused objects, then CLR will clean those objects using Garbage Collector. If your application wants to communicate with other applications, then it will make sure that CTS (Common Type System) is available. CLR uses CAS (Code Access Security) if your application has the proper rights to execute. The CLR will load your application and unload your application, etc. So, for better understanding, please have a look at the following image.

Managed and Unmanaged Code in C#

Now, let say, you have used other third party exe in your dot net application like Skype, PowerPoint, Microsoft Excel, etc. These exe are not made in dot net, they are made using other programming languages such as C, C++.

C# Managed and Unmanaged Code

When you use these exe in your application, then these are not run by CLR. Even though you are running these exe in dot net applications, they are going to run under their own environment. For example, if one exe is developed using C or C++, then that exe will run under the C or C++ runtime environment. In the same line, if the exe is created using VB6, then it is going to run under the VB6 runtime environment.

What exactly the managed and unmanaged code in .NET?

The codes which run under the complete control of CLR are called as Managed Code in .NET. These kinds of code (Managed code in C#) are run by dot net runtime environment. If the dot net framework is not installed or if dot net runtime is not available, then these kinds of codes are not going to be executed. CLR will provide all the facilities and features of .NET to the managed code execution like Language Interoperability, Automatic memory management, Exception handling mechanism, code access security, etc.

On the other hand, Skype, PowerPoint, Microsoft Excel does not require dot net runtime, they run under their own environment. So, in short, the code (exe, web app) which not run under the control of CLR is called unmanaged code in .NET. CLR will not provide any facilities and features of .NET to the unmanaged code in C# execution like Language Interoperability, Automatic memory management, Exception handling mechanism, code access security, etc.

In the next article, I am going to discuss the Garbage Collector in detail. Here, in this article, I try to explain the Managed and Unmanaged Code in .NET. I hope you enjoy this article and I hope now you understood what are exactly are Managed Code and Unmanaged Code in .NET Framework.

Leave a Reply

Your email address will not be published. Required fields are marked *