---
title: "Difference between managed code and Unmanaged code"  
description: "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 La"  
author: "priyanka kushwaha"  
published: 2015-03-11  
updated: 2018-02-22  
canonical: https://www.mindstick.com/blog/833/difference-between-managed-code-and-unmanaged-code  
category: ".net"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Difference between managed code and Unmanaged code

In this blog, I’m explaining about [managed code](https://www.mindstick.com/interview/1874/what-is-the-difference-between-managed-code-and-unmanaged-code) and unmanaged code in C#.

\

##### Managed Code:

1. Managed code is executed under the control of CLR ([Common Language Runtime](https://www.mindstick.com/articles/16/common-language-runtime)).

2. There is no explicit [memory allocation](https://www.mindstick.com/forum/159479/what-are-the-differences-between-new-and-malloc-for-memory-allocation-in-c-plus-plus) and de-allocation.

3. There is no explicit call to the [garbage collector](https://www.mindstick.com/forum/158204/what-are-the-benefits-and-drawbacks-of-using-a-garbage-collector-for-memory-management).

Managed code= .NET Programs

\

##### Unmanaged Code:

Code that is directly executed by the [operating system](https://www.mindstick.com/articles/229069/operating-system-development) is [known as](https://answers.mindstick.com/qa/35703/ricky-ponting-is-also-known-as-what) unmanaged code or [unsafe code](https://www.mindstick.com/forum/33851/what-is-unsafe-code-code-using-pointers-in-c-sharp).

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](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) so that its address may be found.

Right click on [your project](https://www.mindstick.com/forum/156583/best-way-to-link-bootstrap-file-in-your-project) >>[properties](https://www.mindstick.com/articles/23331/nootropics-7-different-types-and-their-unique-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{    class Program    {        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:Sita
```

---

Original Source: https://www.mindstick.com/blog/833/difference-between-managed-code-and-unmanaged-code

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
