托管代码真的有必要吗
我只是想知道托管语言相对于本机代码的真正优势是什么
可移植性:C#/Jave 需要为每个平台提供一个 VM 实现,c++ 必须有一个编译器
垃圾收集器: 我们可以在c++中有一个线程来检查内存分配/解分配
反射:也许我们可以与本机代码有一个类似的机制(???)
今天托管代码已经获得了很多受欢迎程度,但是本机代码一般来说,它的优点是更高效,更轻,即:目标平台上没有虚拟机。另一方面,根据我的说法,托管代码的优点并不是很大,我错了吗?托管代码是大多数应用程序遵循的好方法吗?
I'm just wondering what are the really advantages of managed langage over native code
portability: C#/Jave need to have a VM implementation for each plateforms, c++ has to have a compiler
garbage collector: we can have a thread in c++ which check for memory alloc/desaloc
reflexion: maybe we can have a similary mechanism with native code (???)
Today managed code has gain a lot of popularity, however native code has the advantage to be more efficient in general, and lighter ie: there is no VM to have on the target plateform.On the other hand,according to me, the advantage of managed code are not really big, I am wrong ? Is managed code is the good way to follow for the majority of application ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您关于可移植性的声明:
这不完全正确,或者显然是正确的 - C#、VB.NET、Java 等需要一次编译为 IL、字节码等。但是 C++ 需要针对每个平台编译为不同的版本,这也可能需要更改所使用的 DLL(如果有)或管理内存的方法。
至于垃圾收集,我不太了解如何在 C++ 线程上执行此操作;但在托管代码中,您无需执行任何工作即可拥有垃圾收集器。它已经在那里了。此外,在托管语言中,您倾向于编写更少的代码来完成您需要做的事情,这在清晰度方面具有巨大的优势。 (我发现在设计应用程序的 UI 时尤其如此。)此外,可以使用托管代码执行实时完成优化,并且可以对引擎进行更新以使其更好。
托管代码和非托管代码都有几个优点,最终,这取决于您在做什么。我认为大型项目更适合托管代码,需要速度的部分可以使用非托管代码完成。毕竟,非托管库可以在托管代码中使用,反之亦然。
所以,在我看来,是的,你错了。但这是一个有点主观的问题。
Your statement about portability:
Is not quite true, or clearly true - C#, VB.NET, Java, etc. need to be compiled once to IL, bytecode, etc. but C++ needs to be compiled to a different version for each platform, which also may require changes in DLLs used (if any) or methods for managing memory.
As for garbage collection, I don't know much about doing that on a thread in C++; but in managed code, you don't have to do any work to have a garbage collector. It's already there. Also, in managed languages you tend to write a lot less code to do what it is you need to do, and that has a huge advantage in clarity. (I find this is especially true when designing the UI of an application.) Also, optimizations can be done in real time using managed code execution, and updates can be made to the engine to make it better.
There are several advantages to both managed and unmanaged code, and in the end, it depends what you're doing. I think big projects are more suited to managed code, and parts that require speed can be done with unmanaged code. After all, unmanaged libraries can be used in managed code and vice-versa.
So, in my opinion, yes, you're wrong. It's a bit of a subjective question, though.
托管代码是大多数应用程序的最佳方式。原因是它会更快地实现、更简单的单元测试并且更容易维护。
当然,有些应用程序的情况并非如此。必须快速的应用程序或需要对硬件进行特殊访问的应用程序可能应该是本机应用程序。是的,您可以使用本机代码在托管应用程序中重新实现为您提供的服务,但为什么要这样做呢?
Managed code is the best way to go for the majority of applications. The reason is that it will be faster for you to implement, simpler for you to unit test and easier to maintain.
There are certainly applications where this is not the case. Applications that must be fast or applications that need special access to the hardware should probably be native apps. Yes you can re-implement the services provided to you in managed applications using native code, but why would you?