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

查看:32
本文介绍了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?

推荐答案

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.

最大的区别在于实现.

Java 使用类型擦除的概念来实现泛型.简而言之,底层编译类实际上并不是通用的.它们编译为 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,实现泛型一直到字节码.为了支持 2.0 中的泛型,CLR 进行了几次重大更改.好处是性能改进、深度类型安全验证和反射.

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天全站免登陆