Managed code versus unmanaged code

In this section, we will understand the difference between managed and unmanaged code. Recall that we also studied this in Chapter 1, Learning the Basics of C#. Therefore, for a quick recap, we will just revise the concepts that we covered there. 

These concepts apply not just to the C# language, they are also relevant to all languages written in the .NET Framework. The following are some of the differences between managed and unmanaged code:

  • Managed code is executed by the Common Language Runtime (CLR). Due to this, the code is independent of the underlying OS. On the other hand, unmanaged code is code that is executed by the OS directly. 
  • In the case of managed code, the code is independent of the underlying framework or the OS. CLR compiles the code into an Intermediate Language (IL) code, which is then compiled to machine code. IL code consists of an underlying system or the OS on which the program is executing. On the other hand, in the case of unmanaged code, the code is directly compiled to the underlying machine code. 
  • As managed code is executed by the CLR, the .NET Framework provides several built-in capabilities such as garbage collection and type checking exceptions. However, for unmanaged code, as we will learn in this chapter, a programmer needs to explicitly manage memory cleanup activities, which are otherwise done by the garbage collector. 

Now, before we learn how a programmer can manage memory for unmanaged code, let's first understand how garbage collection works in C# and how useful it is.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.20.68