views:

788

answers:

10

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.

+2  A: 

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
+10  A: 

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

superoptimizer
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
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.
superoptimizer
P/Invoke with existing C++ libraries is close to impossible...
milan1612
+3  A: 

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

Victor Hurdugaci
A: 

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

Benny
A: 

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

cxfx
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
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!
cxfx
+11  A: 

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.

Phil Nash
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.
Pratik
A D .Net implementation is in the works. It may be that, in the near future, D can interoperate with .Net.
dsimcha
@dsimcha - that would be very cool
Phil Nash
+2  A: 

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.
nikie
Could you elaborate on point 3 with regards to speech recognition or image processing?
Lopper
Image processing in C++ => http://www.boost.org/doc/libs/1_41_0/libs/gil/doc/index.html
Oliver
A: 

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
A: 

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.

+1  A: 

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