Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

managed code vs unmanaged code

In this article I will try to explain you managed code and unmanaged Code its differences, advantages and disadvantages.

Managed Code

Program/code written within your application domain (i.e. .Net Framework) is, managed code. The program that are within domain are faster.

In Detail:

The code which is written in .net Framework supported language called as managed code.  This code is directly executed by CLR with help of managed code execution. Any program that is written in .NET Framework is managed code.

Managed code uses CLR which to handle memory management, security, exception etc.

Advantages using Managed Code:

1.  Memory management
2.  Thread management
3.  Exception handling
4.  Garbage collection
5.  Security
6.  Base Library for code reusability.
7.  Able to write code in multiple languages.
8.  Performance improvement.
9. Type safety and security.   
10.  Re-usability of Code


1.  New and changing technology it takes time for people to learn that technology
2.  Multi-platform support isn't available from MS and isn't available straight after installing Visual Studio, but with VS 2015 supporting Multi-platform.
3.   Managed code can be slower than native code
4.  NET framework is free to download but Code Editor is costly.
5.  Not suitable for High End Application

Managed Code example:

Following C# code is managed code

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace C4RandDWebApp
   public class ProductDataAccess
      private string conString = "Data Source=.\\;Integrated Security=true;Initial Catalog=WebShop";
      /// <summary>
      /// Get product Details with dataset
      /// </summary>
      Public DataSet GetProductDetails()
         var con = new SqlConnection(conString);
         var da = new SqlDataAdapter("GetProductDetails", con)
            SelectCommand =
               CommandType = CommandType.StoredProcedure
         var ds = new DataSet();
         da.Fill(ds, "Products");
         return ds;


Unmanaged Code

The program, code written in language that is not supported by .net Framework called as Unmanaged code.  

Code written in C language or C++ which is not supported by .net framework is unmanaged code.

In Detail:

Applications that do not run under the control of the CLR are said to be unmanaged, and certain languages such as C,C++ can be used to write such applications, which, for example, access low - level functions of the operating system. Background compatibility with code of VB, ASP and COM are examples of unmanaged code.

Unmanaged code is executed with help of wrapper classes.

 There are two types of Wrapper classes:

1.  CCW (COM Callable Wrapper)
2.  RCW (Runtime Callable Wrapper).

Advantages using:

1.  Easy to learn and write code.
2.  You can execute code in any platform with some helper classes (for example Wrapper classes).
3.  Native code should be faster and have a smaller memory footprint than managed code, reason lot of things are happening on behind with Garbage collection, Type safety.
4. Maintaining native code is easy compared with managed code.

Disadvantages using:

1.  You may face memory leak and that leads to major performance issues. 
2.  Handling explicitly memory management.
 3.  Type safety, array bound and index checking, for example variable data type casting from one to other is hard in unmanaged code.
4.  No default support for exception handling, you require to explicitly handle exceptions.
5. Code security issues.

Unmanaged code example:

void main()
              char c,myfname[10];
              ofstream out;
              cout<<"Enter File name:";
              cin>> myfname;
              cout<<"Enter data that’s is to be stored in file (Enter # at end):\n";

Choosing managed or unmanaged code:

It is better to use hardware related application using native code.
For Web application, Finance applications better to use managed code.



Collection was modified

When I am trying to remove element from generic list using foreach loop, I am facing following issue.


Collection was modified; enumeration operation may not execute.

My code:

  if (duplicateUsers != null && duplicateUsers.Count()> 1)
             foreach (var duplicateUserData in duplicateUsers)


If you are using generic list then simply use extension method ToList() with list i.e

duplicateUsers .ToList()

Your code should be:

  if (duplicateUsers != null && duplicateUsers.Count()> 1)
             foreach (var duplicateUserData in duplicateUsers.ToList())


Join clause with linq query

why join clause with linq query?

Join clause is used for comparing elements from different source sequences that have no direct relationship in the object model.

Points to remember:

1.  Ensure different source sequences contain matching value that can be compared for equality
2.  The join clause compares the specified keys for equality by using the special equal’s keyword.
3.  All joins performed by the join clause are equijoins.

Following 6 most common join types you can perform using join clause

1.  Inner join
2.  Group join
3.   Right outer join
4.  Left outer join
5.  Full outer join
6.  self join


Inner join example:              

  //inner join linq query returns only matching records
var InnerJoinProducts = (from p in DbContext.Product
join c in DbContext.Category 
on new { PID = p.CategoryID } equals new { PID =   c.CategoryID }
select p

For full outer join, self-join examples you can check following link

Group join example:
                //List of products group by mathcing categoryid and Price less than 500
//Group join
var productGroupWithValidPrice =( from category in DbContext.Category
join prod in DbContext.Product on category.CategoryID equalsprod.CategoryID into prodGroup
from productsData in prodGroup
where productsData.UnitPrice > 5000
select productsData).ToList();

for Right outer join example you can check following link

 Left outer join example:

var products = (from p in DbContext.Product
join c in DbContext.Category
on new { PID = p.CategoryID } equals new { PID = c.CategoryID }
into ProductInfo
from  productsData in ProductInfo.DefaultIfEmpty()
select new
                                      ProductID = p.ProductID,
                                      ProductName = (p.ProductName == 
null)? null:p.ProductName,
                                      Description =  (p.Description == 
                                      Description =  (p.Description == 
null)? null:p.Description,
                                      ImagePath = (p.ImagePath == 
                                      ImagePath = (p.ImagePath == 
null)? null:p.ImagePath,
                                      UnitPrice = (p.UnitPrice == 
                                      UnitPrice = (p.UnitPrice == 
null)? 0:p.UnitPrice,
                                      CategoryID = (p.CategoryID == 
                                      CategoryID = (p.CategoryID == 
null)? 0:p.CategoryID