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

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

最大的区别,虽然是在执行。

The biggest difference though is in the implementation.

Java使用类型擦除的概念来实现仿制药。总之底层编译的类实际上并不是通用的。他们搜集到对象和转换。实际上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的,实行仿制药,他们都一路下跌到字节code。在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天全站免登陆