blog

Home / DeveloperSection / Blogs / Difference between managed code and Unmanaged code

Difference between managed code and Unmanaged code

priyanka kushwaha4366 11-Mar-2015

In this blog, I’m explaining about managed code and unmanaged code in C#.


Managed Code:               

1.    Managed code is executed under the control of CLR (Common Language Runtime).

2.   There is no explicit memory allocation and de-allocation.

3.   There is no explicit call to the garbage collector.

      Managed code= .NET Programs


Unmanaged Code:

Code that is directly executed by the operating system is known as unmanaged code or unsafe code.

Unsafe code is code which does not execute under full control CLR. It can cause some problems, therefore, each use must be marked as unsafe.

Fixed: In an unsafe context, the fixed keyword can be used to temporarily fix a variable so that its address may be found.

Right click on your project >>properties>> go to the build tab>> check unsafe code checkbox

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace UnsafeCodeExample
{
    classProgram
    {
        unsafe static void Main(string[] args)
        {
            string str="Sita";
            fixed (char* value = str)
            { 
                char* ptr = value;
                while(*ptr != '\0')
                {
                    Console.WriteLine(*ptr);
                    ptr++;
                }
            }
            Console.ReadKey();
        }
   }
}
Output:
S
i
t
a

Updated 22-Feb-2018

Leave Comment

Comments

Liked By