9

Is there any major advantage of managed C++/CLI over C#. Definitely not the syntax I suppose as the following code in C++/CLI is real ugly,

C++/CLI code:

[Out]List<SomeObject^>^% someVariable

Compare above with C# Code:

out List<SomeObject> someVariable

Just out of curiosity, is there an even uglier syntax in C++/CLI as compared to the above.

Mat
  • 202,337
  • 40
  • 393
  • 406
Lopper
  • 3,499
  • 7
  • 38
  • 57

11 Answers11

13

It's almost exclusively an interopability language - both for allowing .Net code to access legacy C++ libraries, or for extended existing (native) C++ code bases with access to .Net libraries (and some variations on these themes).

While it is possible to write fully fledged applications solely in C++/CLI, and it even gives you some language features not available in pure C++ (such as garbage collection), I doubt there are many people who would actually do this. If you're already moving away from pure C++ and don't have the goal of interop with .Net there are probably more natural choices (such as D or Scala, for example - depending on which direction you want to go in).

Similarly, moving from pure C# to C++/CLI could arguably bring the advantages of C++ templates, but it's rare that this need would lead to you taking that step.

philsquared
  • 22,403
  • 12
  • 69
  • 98
  • 1
    I can add to support this. In a previous project I had to write a interface between a C# asmx web service application and IBM middleware that only had a C++ api library (not a dll but a static lib). C++/CLI was the only choice to create this bridge. – softveda Dec 11 '09 at 11:24
  • 1
    A D .Net implementation is in the works. It may be that, in the near future, D can interoperate with .Net. – dsimcha Dec 22 '09 at 19:22
12

Easier interoperation with native C++ code is the one advantage.

Whether it's a major advantage is subjective.

Unless you want to mingle with existing native C++ code, you're probably much better off with C#.

  • Good answer. But it's not really subjective - it's a major advantage if (and only if) you have no choice but to deal with existing native code. – OregonGhost Dec 11 '09 at 09:39
  • 3
    It's still subjective. If what you need is to use an existing DLL, you could choose to interoperate with existing code using C# and using P/Invoke. Whether it's a "major" advantage or "minor" advantage to you to have the easier interop vs. a "major" advantage or "minor" advantage to have C# is completely up to the person asking the question. –  Dec 11 '09 at 09:45
6

I can think of 3 main reasons to use C++/CLI:

  1. You already have a large C++ project and want to use .NET in it (whether you want to migrate it completely in the future or not)
  2. You want to use a library written in C or C++. For simple libraries, you can use C#/PInvoke, but e.g. if the libary comes with a complex type system, you might be better off creating C++/CLI wrappers instead of recreating the type system in C#
  3. Parts in your project are best written in C++. E.g. if you're doing speech recognition or image processing, C++ might simply be better suited for the task.
Niki
  • 15,662
  • 5
  • 48
  • 74
6

Being able to use native headers files directly is a huge advantage, but not the only one.

Stack semantics are so much better than anything C# has to offer for IDisposable management. C++/CLI has one uniform syntax for correctly managing variables which are IDisposable and those which aren't, both as local variables and as member fields. Comparison:

ref class MyClass
{
   FileStream fs;
}

vs

class MyClass : IDisposable
{
  FileStream fs;

  void IDisposable.Dispose() { Dispose(true); }
  ~MyClass() { Dispose(false); }

  public virtual void Dispose(bool disposing) { if (disposing) fs.Dispose(); }
}

Now which language is looking ugly?

Then there are templates, interior_ptr, #define, native DLL exports, pointer-to-member, and probably several other things I've forgotten.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • I agree, except #define. From my point of view its immense and indifferent usage in a lot of projects is a PITA. – Alex Feb 10 '12 at 12:33
3

Using C++/CLI it is much easy to interact with native C++ code

Victor Hurdugaci
  • 28,177
  • 5
  • 87
  • 103
2

The advantage of managed C++ is that it is easy to mix managed and unmanaged code. But if all (or almost all) of your code will be managed, then C# should definitely be used (and you can still invoking unmanaged code from C# using the DllImport attribute).

Konamiman
  • 49,681
  • 17
  • 108
  • 138
2

CLI/C++ has many advantages over C#.

  1. STD libraries
  2. Native C++/C cannot be viewed by disassembler (like Reflector) because they are not actually CLI (no need to obfuscate (although a good hacker can already get by this)).
  3. Mingling C/C++ projects as a wrapper to be used with .Net languages. C++/CLI is not a language is just adds support for .Net with C/C++.
  4. Some bit of control on memory via C/C++ pointer pointing to C/C++ objects on the heap.

I hate having to trust in GC to get to an object stuck on the gen 2. Lord knows when that will be released from the managed heap.

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
Robert
  • 21
  • 1
1

Being a predominantly C# programmer, I was finding myself having to use C++/CLI with a bit of pain. However, as an interop language, it FAR outweighs C# for having to work with native code. The C++/CLI IDE in Visual Studio lacks a lot of the features in the C# flavor.

Overall, it has its place and will remain viable as long as native code exists. I wouldn't want to have to create WinForm apps from scratch with the C++/CLI IDE if I didn't have to.

0

you just turn to c++\cli when you have to, if you can meet you requirement using c#, why bother go c++\cli

Benny
  • 8,547
  • 9
  • 60
  • 93
0

Generally, I think the main advantage of C++/CLI is simply familiarity for C++ developers. If you're not coming from a C++ background then go with C#.

Chris Fulstow
  • 41,170
  • 10
  • 86
  • 110
  • I think it's not. I'm coming from a C++ background (mixed with Java and Delphi), and C# was so simple to grasp. No need for C++/CLI just for familiarity. I was rather lucky to get away from C++ syntax :) – OregonGhost Dec 11 '09 at 09:41
  • Interesting, it's been a while since I did any C++ but I assumed the C++/CLI syntax would be more familiar than C#, but I guess not! – Chris Fulstow Dec 11 '09 at 09:47
0

unmanaged c++ applications do not need a framework to run, c# will only run on machines with dotnet framework 1, 2, 3 or 4. it surprising how many machines still run without these framework.

besi
  • 1
  • 1