C++/CLI:相对于 C# 的优势
与 C# 相比,托管 C++/CLI 有什么主要优势吗?绝对不是我认为的语法,因为 C++/CLI 中的以下代码非常丑陋,
C++/CLI 代码:
[Out]List<SomeObject^>^% someVariable
将上面与 C# 代码进行比较:
out List<SomeObject> someVariable
只是出于好奇,与上面相比,C++/CLI 中是否有更丑陋的语法。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
它几乎完全是一种互操作性语言 - 既允许 .Net 代码访问旧版 C++ 库,也允许扩展现有(本机)C++ 代码库以访问 .Net 库(以及这些主题的一些变体)。
虽然仅用 C++/CLI 编写成熟的应用程序是可能的,而且它甚至为您提供了纯 C++ 中不具备的一些语言功能(例如垃圾收集),但我怀疑有很多人会这样做实际上这样做。如果您已经放弃纯 C++ 并且没有与 .Net 互操作的目标,那么可能有更自然的选择(例如 D 或 Scala - 取决于您想要的方向进去)。
同样,从纯 C# 迁移到 C++/CLI 可以说可以带来 C++ 模板的优势,但这种需求很少会导致您采取这一步。
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.
与本机 C++ 代码的互操作更容易是其中一个优点。
是否是主要优势是主观的。
除非您想与现有的本机 C++ 代码混合,否则使用 C# 可能会更好。
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#.
我可以想到使用 C++/CLI 的 3 个主要原因:
I can think of 3 main reasons to use C++/CLI:
能够直接使用本机头文件是一个巨大的优势,但不是唯一的优势。
堆栈语义比 C# 提供的 IDisposable 管理要好得多。 C++/CLI 有一种统一的语法来正确管理 IDisposable 变量和非 IDisposable 变量,无论是作为局部变量还是作为成员字段。比较:
vs
现在哪种语言看起来很丑?
然后还有模板、
interior_ptr
、#define
、本机 DLL 导出、指向成员的指针,可能还有其他一些我已经忘记的东西。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 areIDisposable
and those which aren't, both as local variables and as member fields. Comparison:vs
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.使用 C++/CLI 可以很容易地与本机 C++ 代码交互
Using C++/CLI it is much easy to interact with native C++ code
托管C++的优点是很容易混合托管和非托管代码。但是,如果您的所有(或几乎所有)代码都将被管理,那么绝对应该使用 C#(并且您仍然可以 使用 DllImport 属性)。
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).
CLI/C++ 比 C# 有很多优点。
我讨厌必须相信 GC 才能到达卡在第 2 代上的对象。上帝知道什么时候会从托管堆中释放该对象。
CLI/C++ has many advantages over C#.
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.
作为一名主要使用 C# 的程序员,我发现自己在使用 C++/CLI 时感到有点痛苦。然而,作为一种互操作语言,它在必须使用本机代码方面远胜于 C#。 Visual Studio 中的 C++/CLI IDE 缺乏 C# 风格的许多功能。
总的来说,只要本机代码存在,它就有其地位,并且将保持可行。如果不需要的话,我不想使用 C++/CLI IDE 从头开始创建 WinForm 应用程序。
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.
必要的时候你就转向 c++\cli ,如果你能用 c# 满足你的要求,为什么还要去 c++\cli
you just turn to c++\cli when you have to, if you can meet you requirement using c#, why bother go c++\cli
一般来说,我认为 C++/CLI 的主要优点是 C++ 开发人员很熟悉。如果您没有 C++ 背景,那么就选择 C#。
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#.
非托管 C++ 应用程序不需要框架来运行,C# 只能在具有 dotnet 框架 1、2、3 或 4 的计算机上运行。令人惊讶的是,有多少计算机仍然在没有这些框架的情况下运行。
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.