C++/CLI : Advantages over C#

前端 未结 11 2214
自闭症患者
自闭症患者 2021-01-05 00:54

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:

<         


        
相关标签:
11条回答
  • 2021-01-05 01:09

    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.
    0 讨论(0)
  • 2021-01-05 01:12

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

    0 讨论(0)
  • 2021-01-05 01:17

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

    0 讨论(0)
  • 2021-01-05 01:18

    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.

    0 讨论(0)
  • 2021-01-05 01:21

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

    0 讨论(0)
提交回复
热议问题