Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between managed C++ and C#?

The major advantage I see for using C++ instead of C# is compiling to native code, so we get better performance. C# is easier, but compiles to managed code.

Why would anyone use managed C++ for? What advantages it gives us?

like image 543
Doug Avatar asked Jan 28 '10 01:01

Doug


People also ask

What is difference between managed and unmanaged code?

Difference between managed and unmanaged code? Managed code is the one that is executed by the CLR of the . NET framework while unmanaged or unsafe code is executed by the operating system. The managed code provides security to the code while undamaged code creates security threats.

Is C++ managed or unmanaged?

When not specified, C++ is unmanaged C++, compiled to machine code. In unmanaged C++ you must manage memory allocation manually. Managed C++ is a language invented by Microsoft, that compiles to bytecode run by the . NET Framework.

What is the difference between C++ and C++/CLI?

C++ runs directly as binary complied for your hardware. C++ cli is a c++ extension that is used to interface with the MS common language runtime. It complies to IL normally and is executed inside the . net runtime.

What is managed code in C sharp?

To put it very simply, managed code is just that: code whose execution is managed by a runtime. In this case, the runtime in question is called the Common Language Runtime or CLR, regardless of the implementation (for example, Mono, . NET Framework, or . NET Core/.


2 Answers

Managed C++ and C++/CLI allow you to easily write managed code that interacts with native C++.

This is especially useful when migrating an existing system to .Net and when working in scientific contexts with calculations that must be run in C++.

like image 83
SLaks Avatar answered Sep 20 '22 23:09

SLaks


Managed c++ allows to more easily interop between native code, and managed code. For instance, if you have a library in c++ (.cpp files and .h files), you can link them into your project, and create the appropriate CLR objects, and simply call the native code from within your CLR objects:

#include "yourcoollibrary.h"

namespace DotNetLibraryNamespace
{
    public ref class DotNetClass
    {
    public:
        DotNetClass()
        {
        }

        property System::String ^Foo
        {
            System::String ^get()
            {
                return gcnew System::String(c.data.c_str());
            }
            void set(System::String ^str)
            {
                marshal_context ctx;
                c.data = ctx.marshal_as<const char *>(str);
            }
        }

    private:
        NativeClassInMyCoolLibrary c;
    };
}
like image 44
FryGuy Avatar answered Sep 17 '22 23:09

FryGuy