Doug
Doug

Reputation: 6442

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?

Upvotes: 6

Views: 1006

Answers (3)

Sorlaize
Sorlaize

Reputation: 21

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

Upvotes: 0

FryGuy
FryGuy

Reputation: 8744

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;
    };
}

Upvotes: 2

SLaks
SLaks

Reputation: 887365

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

Upvotes: 7

Related Questions