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:
<
I can think of 3 main reasons to use C++/CLI:
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#.
Using C++/CLI it is much easy to interact with native C++ code
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.
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).