C++/CLI:相对于 C# 的优势

发布于 2024-08-13 20:56:06 字数 290 浏览 5 评论 0原文

与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

笨死的猪 2024-08-20 20:56:06

它几乎完全是一种互操作性语言 - 既允许 .Net 代码访问旧版 C++ 库,也允许扩展现有(本机)C++ 代码库以访问 .Net 库(以及这些主题的一些变体)。

虽然仅用 C++/CLI 编写成熟的应用程序是可能的,而且它甚至为您提供了纯 C++ 中不具备的一些语言功能(例如垃圾收集),但我怀疑有很多人会这样做实际上这样做。如果您已经放弃纯 C++ 并且没有与 .Net 互操作的目标,那么可能有更自然的选择(例如 DScala - 取决于您想要的方向进去)。

同样,从纯 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.

燃情 2024-08-20 20:56:06

与本机 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#.

嗼ふ静 2024-08-20 20:56:06

我可以想到使用 C++/CLI 的 3 个主要原因:

  1. 您已经有一个大型 C++ 项目,并且希望在其中使用 .NET(无论您是否希望将来完全迁移它)
  2. 您希望使用用 .NET 编写的库C 或 C++。对于简单的库,您可以使用 C#/PInvoke,但例如,如果库附带复杂的类型系统,您最好创建 C++/CLI 包装器,而不是在 C# 中重新创建类型系统。
  3. 项目中的部分最好用 C++ 编写。例如,如果您正在进行语音识别或图像处理,C++ 可能更适合该任务。

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.
初心 2024-08-20 20:56:06

能够直接使用本机头文件是一个巨大的优势,但不是唯一的优势。

堆栈语义比 C# 提供的 IDisposable 管理要好得多。 C++/CLI 有一种统一的语法来正确管理 IDisposable 变量和非 IDisposable 变量,无论是作为局部变量还是作为成员字段。比较:

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(); }
}

现在哪种语言看起来很丑?

然后还有模板、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 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.

熊抱啵儿 2024-08-20 20:56:06

使用 C++/CLI 可以很容易地与本机 C++ 代码交互

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

相守太难 2024-08-20 20:56:06

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

五里雾 2024-08-20 20:56:06

CLI/C++ 比 C# 有很多优点。

  1. STD 库
  2. Native C++/C 无法通过反汇编程序(如 Reflector)查看,因为它们实际上不是 CLI(无需混淆(尽管优秀的黑客已经可以做到这一点))。
  3. 将 C/C++ 项目混合为与 .Net 语言一起使用的包装器。 C++/CLI 不是一种语言,只是通过 C/C++ 添加了对 .Net 的支持。
  4. 通过指向堆上 C/C++ 对象的 C/C++ 指针对内存进行一些控制。

我讨厌必须相信 GC 才能到达卡在第 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.

×纯※雪 2024-08-20 20:56:06

作为一名主要使用 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.

并安 2024-08-20 20:56:06

必要的时候你就转向 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

醉殇 2024-08-20 20:56:06

一般来说,我认为 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#.

她如夕阳 2024-08-20 20:56:06

非托管 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文