views:

290

answers:

7

If you had to decide between C# and Managed C++, which would you choose and why?

Are there benefits of Managed C++ over C#? Which language do you prefer? What decisions would you make under what circumstances?

+3  A: 

Managed C++ is good for interop with C++: for example, if you have a .NET application and your assembly has to interact with a native interface that comes as C++ .lib files (which I had more than once), or with a nice C++ API.

Example: Rithmic (not that you ever heard of them) until recently ONLY supported a C++ API. If you try to access them from C# - good luck ;) Managed C++ allows me to access their API and expose nice .NET objects.

Basically interop. Managed C++ REALLY shines in interop with low level C / C++ API's.

TomTom
+1 I read your answer before it was edited, thanks for the little more "feeling" inside it ;). however thanks to ChrisW for editing it to be more SO-appropriate.
Oops
+15  A: 

I would use managed C++ if I:

  • Needed to integrate with existing C/C++ code
  • Needed to port existing C/C++ code to .net
  • Needed to use .NET objects from C++
  • Needed to expose .NET object over COM in a more complex way than what .net makes easy
  • Needed to access hardware directly
  • Needed to call lots of unmanaged APIs

And already had some skills in C++, as the above tasks will need a experienced C++ programmer. Most of the time I would only consider managed C++ if the company already had a C++ code base, otherwise who is going to maintain the managed C++ code.

In other cases I would always choose C#. Even if I choose managed C++ for part of the system, I would try to write as much of the system as possible in C#.

Think of manage C++ as a bridge building kit for going between the unmanaged world of C/C++ and the managed world of .NET.

If you only need to call a few simple APIs from C#, then see pinvoke.net and this overview to find how to call them from C#, as a few lines of complex pinvoke code (prebuilt bridge) in C# is normally better then introducing C++ to a project that is not already using it.

Ian Ringrose
A: 

I would prefer C#, or specifically .NET, over C++ because of the extensive .NET standard library.

Sjoerd
The question refers to Managed C++ which has access to the .NET library, so your answer doesn't help.
Foole
Ok, I misunderstood. Thanks.
Sjoerd
+2  A: 

I used managed C++ when I needed to build up new NET component with much ofC++ unmanaged code inside. I did a specific class used to Marshall some objects forward and back from old C++ code.

Arseny
A: 

I haven't personally written, or read for that matter, too many lines of code in managed C++, but from what I have seen it looks too convoluted and patchy. That said, you might want to use managed C++ if you are really good in C++, and when learning the idioms and patterns of a new language would be too much of a risk.

Use C# if you are quite competent in it. If you are only getting started with C++ and C#, I think, C# is the easier route to take.

tathagata
+2  A: 

I've encountered a problem which was transparent in managed C++, but made a big headache in C# - I had to send a callback function to a C++ unmanaged library, that defined this callback as __cdecl. In C#, the default calling convention is __stdcall, and it is pretty compilcated to move a C# delegate from __stdcall to __cdecl (Actually it requires either a 'shim' unmanaged DLL to do so or using some reflection classes). In C++, (C++.Net as well), it is just a simple attribute.

rursw1
+4  A: 

what is the benefit of managed C++ over C#?

C++.net is useful for interacting with C++ and C code (that is, calling external C or C++ libraries, providing callback functions to external modules written in C or C++ and so on.

what language of both would you prefer?

I would prefer C# for all situations except the one described above (interacting with C and C++).

C# is easier to write, simpler, and geared specifically to use the .NET platform. C++ can do it also, but it has all the complexity of the C++ language plus the extensions needed to use the .NET platform.

Unless you need to interact with native C++ or C code, you're better off using C# in most cases (that is, if you're coding for the .NET platform).

Normally I prefer C++, but when needing to code for .NET, it doesn't beat C#.

utnapistim