views:

160

answers:

3

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?

+5  A: 

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++.

SLaks
..and C# allows you to easily write the _rest_ of the code.
Marc Bollinger
+2  A: 

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;
    };
}
FryGuy
A: 

(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!

Sorlaize