我应该使用托管 C++还是 C# 适合我的应用程序?

发布于 2024-09-07 19:09:33 字数 94 浏览 5 评论 0原文

如果您必须在 C# 和托管 C++ 之间做出选择,您会选择哪一个?为什么?

托管 C++ 比 C# 有优势吗?您更喜欢哪种语言?在什么情况下你会做出什么决定?

If you had to decide between C# and Managed C++, which would you choose and why?

Are there benefits of Managed C++ over C#? Which language do you prefer? What decisions would you make under what circumstances?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

染柒℉ 2024-09-14 19:09:33

如果满足以下条件,我将使用托管 C++:

  • 需要与现有 C/C++ 代码集成
  • 需要将现有 C/C++ 代码移植到 .net
  • 需要使用 C++ 中的 .NET 对象
  • 需要以比 COM 更复杂的方式公开 .NET 对象.net 使什么变得容易
  • 需要直接访问硬件
  • 需要调用大量非托管 API

并且 已经具备一些 C++ 技能,因为上述任务需要经验丰富的 C++ 程序员。大多数时候,如果公司已经有 C++ 代码库,我只会考虑托管 C++,否则谁来维护托管 C++ 代码。

在其他情况下,我总是会选择 C#。即使我选择托管 C++ 作为系统的一部分,我也会尝试用 C# 编写尽可能多的系统。

将管理 C++ 视为一个桥梁构建工具,用于在
C/C++ 的非托管世界以及
.NET 的托管世界。

如果您只需要从 C# 调用一些简单的 API,请参阅 pinvoke.net此概述 了解如何从 C# 调用它们,如几行复杂的 pinvoke 代码 ( C# 中的预构建桥)通常比将 C++ 引入尚未使用它的项目更好。

I would use managed C++ if I:

  • Needed to integrate with existing C/C++ code
  • Needed to port existing C/C++ code to .net
  • Needed to use .NET objects from C++
  • Needed to expose .NET object over COM in a more complex way than what .net makes easy
  • Needed to access hardware directly
  • Needed to call lots of unmanaged APIs

And already had some skills in C++, as the above tasks will need a experienced C++ programmer. Most of the time I would only consider managed C++ if the company already had a C++ code base, otherwise who is going to maintain the managed C++ code.

In other cases I would always choose C#. Even if I choose managed C++ for part of the system, I would try to write as much of the system as possible in C#.

Think of manage C++ as a bridge building kit for going between the
unmanaged world of C/C++ and the
managed world of .NET.

If you only need to call a few simple APIs from C#, then see pinvoke.net and this overview to find how to call them from C#, as a few lines of complex pinvoke code (prebuilt bridge) in C# is normally better then introducing C++ to a project that is not already using it.

黑色毁心梦 2024-09-14 19:09:33

托管C++有什么好处
通过 C#?

C++.net 对于与 C++ 和 C 代码交互非常有用(即调用外部 C 或 C++ 库,为用 C 或 C++ 编写的外部模块提供回调函数等等。

您会使用哪种语言
更喜欢?

除了上述情况(与 C 和 C++ 交互)之外,我更喜欢在所有情况下使用 C#。

C# 更容易编写、更简单,并且专门适合使用 .NET 平台。 C++ 也可以做到这一点,但它具有 C++ 语言的所有复杂性以及使用 .NET 平台所需的扩展。

除非您需要与本机 C++ 或 C 代码交互,否则在大多数情况下最好使用 C#(即,如果您正在为 .NET 平台编码)。

通常我更喜欢 C++,但当需要为 .NET 编写代码时,它并不能胜过 C#。

what is the benefit of managed C++
over C#?

C++.net is useful for interacting with C++ and C code (that is, calling external C or C++ libraries, providing callback functions to external modules written in C or C++ and so on.

what language of both would you
prefer?

I would prefer C# for all situations except the one described above (interacting with C and C++).

C# is easier to write, simpler, and geared specifically to use the .NET platform. C++ can do it also, but it has all the complexity of the C++ language plus the extensions needed to use the .NET platform.

Unless you need to interact with native C++ or C code, you're better off using C# in most cases (that is, if you're coding for the .NET platform).

Normally I prefer C++, but when needing to code for .NET, it doesn't beat C#.

懷念過去 2024-09-14 19:09:33

托管 C++ 非常适合与 C++ 进行互操作:例如,如果您有一个 .NET 应用程序,并且您的程序集必须与作为 C++ .lib 文件(我不止一次)提供的本机接口交互,或者与一个不错的 C++ 交互API。

示例:Rithmic(您可能没有听说过它们)直到最近才支持 C++ API。如果您尝试从 C# 访问它们 - 祝您好运;)托管 C++ 允许我访问它们的 API 并公开漂亮的 .NET 对象。

基本上是互操作的。托管 C++ 在与低级 C / C++ API 的互操作方面确实表现出色。

Managed C++ is good for interop with C++: for example, if you have a .NET application and your assembly has to interact with a native interface that comes as C++ .lib files (which I had more than once), or with a nice C++ API.

Example: Rithmic (not that you ever heard of them) until recently ONLY supported a C++ API. If you try to access them from C# - good luck ;) Managed C++ allows me to access their API and expose nice .NET objects.

Basically interop. Managed C++ REALLY shines in interop with low level C / C++ API's.

眼睛会笑 2024-09-14 19:09:33

当我需要构建新的 NET 组件并在其中包含大量 C++ 非托管代码时,我使用了托管 C++。
我做了一个特定的类,用于从旧的 C++ 代码中向前和向后编组一些对象。

I used managed C++ when I needed to build up new NET component with much ofC++ unmanaged code inside.
I did a specific class used to Marshall some objects forward and back from old C++ code.

风吹雪碎 2024-09-14 19:09:33

我遇到了一个问题,该问题在托管 C++ 中是透明的,但在 C# 中却令人头疼 - 我必须将回调函数发送到 C++ 非托管库,该库将此回调定义为 __cdecl。在 C# 中,默认调用约定是 __stdcall,将 C# 委托从 __stdcall 移动到 __cdecl 非常复杂(实际上,它需要“shim”非托管 DLL 才能执行此操作,或者使用一些反射类)。
在 C++(以及 C++.Net)中,它只是一个简单的属性。

I've encountered a problem which was transparent in managed C++, but made a big headache in C# - I had to send a callback function to a C++ unmanaged library, that defined this callback as __cdecl. In C#, the default calling convention is __stdcall, and it is pretty compilcated to move a C# delegate from __stdcall to __cdecl (Actually it requires either a 'shim' unmanaged DLL to do so or using some reflection classes).
In C++, (C++.Net as well), it is just a simple attribute.

日暮斜阳 2024-09-14 19:09:33

我个人没有在托管 C++ 中编写或阅读过太多行代码,但从我所看到的来看,它看起来太复杂且不完整。也就是说,如果您真的很擅长 C++,那么您可能希望使用托管 C++,并且在学习新语言的习惯用法和模式时风险太大。

如果您非常擅长 C#,请使用它。如果您刚刚开始使用 C++ 和 C#,我认为 C# 是更容易选择的途径。

I haven't personally written, or read for that matter, too many lines of code in managed C++, but from what I have seen it looks too convoluted and patchy. That said, you might want to use managed C++ if you are really good in C++, and when learning the idioms and patterns of a new language would be too much of a risk.

Use C# if you are quite competent in it. If you are only getting started with C++ and C#, I think, C# is the easier route to take.

沉睡月亮 2024-09-14 19:09:33

与 C++ 相比,我更喜欢 C#,特别是 .NET,因为它拥有丰富的 .NET 标准库。

I would prefer C#, or specifically .NET, over C++ because of the extensive .NET standard library.

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