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?
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.
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.
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.
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/.
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++.
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;
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With