11

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?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
OlimilOops
  • 6,747
  • 6
  • 26
  • 36
  • 2
    I tried both `C++.NET` and `C#`: Only `C++.NET` gave me that much headaches and "WTF!". It might have it's uses, but mixing three layers (`C`, `C++`, `.NET`) really makes it bad looking imo. – ereOn Jun 30 '10 at 09:00
  • 2
    this MUST have been asked before, but I can't find it on Stackoverflow – Ian Ringrose Jun 30 '10 at 09:12
  • @Ian yes I tried to search for it. But... I feel finding someting on SO is really hard due to the mass of questions there are already – OlimilOops Jun 30 '10 at 09:15
  • 3
    Gang, the managed variant of C++ is called C++/CLI. Once upon a time there was something called Managed C++ which was quite horrible. And there never was a C++.NET. – Kate Gregory Jun 30 '10 at 12:33
  • There's no way to objectively answer this question given the parameters. – George Stocker Oct 05 '10 at 15:53

7 Answers7

22

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
  • 51,220
  • 55
  • 213
  • 317
5

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
  • 26,809
  • 3
  • 46
  • 82
4

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.

ChrisW
  • 54,973
  • 13
  • 116
  • 224
TomTom
  • 61,059
  • 10
  • 88
  • 148
  • +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. – OlimilOops Jun 30 '10 at 09:25
2

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
  • 7,251
  • 4
  • 37
  • 52
2

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.

rkellerm
  • 5,362
  • 8
  • 58
  • 95
1

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
  • 478
  • 3
  • 12
0

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

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
  • 3
    The question refers to Managed C++ which has access to the .NET library, so your answer doesn't help. – Foole Jun 30 '10 at 09:02