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 easi开发者_StackOverflower, but compiles to managed code.
Why would anyone use managed C++ for? What advantages it gives us?
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;
};
}
(c++/cli is the new name) You can wrap native code to work flawlessly with garbage controlled c# and even process callbacks too. Inversely you can create managed types and interact with them from c++.
Allows developers to migrate to c# easily to pilot fast build times and so on, e.g. xna, linking to native libraries, as mentioned!
精彩评论