C#和Java泛型 [英] C# vs Java generics

查看:102
本文介绍了C#和Java泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说泛型的Java实现不如C#实现好。因为语法看起来类似,关于Java实现是不合格的,还是一个宗教的观点?

I have heard that the Java implementation of Generics is not as good as the C# implementation. In that the syntax looks similar, what is it that is substandard about the Java implementation, or is it a religious point of view?

推荐答案

p> streloksi的链接是一个伟大的工作,打破降低差异。快速和脏的总结虽然是...

streloksi's link does a great job of breaking down the differences. The quick and dirty summary though is ...

在语法和使用方面。语言之间的语法大致相同。这里和那里的几个怪癖(最显着的约束)。但基本上如果你可以读一个,你可以阅读/使用其他。

In terms of syntax and usage. The syntax is roughly the same between the languages. A few quirks here and there (most notably in constraints). But basically if you can read one, you can likely read/use the other.

最大的区别是在实现。

The biggest difference though is in the implementation.

Java使用类型erasure的概念来实现泛型。简而言之,底层编译的类实际上不是通用的。他们编译成Object并强制转换。实际上,Java泛型是一个编译时工件,在运行时很容易被破坏。

Java uses the notion of type erasure to implement generics. In short the underlying compiled classes are not actually generic. They compile down to Object and casts. In effect Java generics are a compile time artifact and can easily be subverted at runtime.

C#另一方面,凭借CLR,实现泛型,所有它们直到字节码。 CLR采取了几个突破性的变化,以支持泛型在2.0。好处是性能改进,深层安全验证和反思。

C# on the other hand, by virtue of the CLR, implement generics all they way down to the byte code. The CLR took several breaking changes in order to support generics in 2.0. The benefits are performance improvements, deep type safety verification and reflection.

同样,提供的链接有更深入的分析,我鼓励你阅读

Again the provided link has a much more in depth breakdown I encourage you to read

这篇关于C#和Java泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆