我研究了Java很长时间,并且非常熟悉该语言中泛型类型的操作:我知道只有在编译时才会有类型删除,因此在运行时这些信息不会被删除。可用)并了解在泛型类型上应用多态性时的困难。
现在我正在学习 C#,我注意到虽然这种语言使用类似的表示法(类型)语义似乎并不相同。例如,看看这个问题,C# 运行时是否像 Java 那样存储泛型类型的信息而不是丢弃它,这是正确的吗?另外,我从未在 C# 中见过使用“jokers”(通配符)和类型 < ? 的示例。扩展 TipoGenerico > .这在该语言中是可能的(甚至是必要的)?
最后,C# 支持单个方法(不仅仅是类)泛型?如果是,Java 中此构造的等效语法是什么:
public <T> void method (T parameter1 , parameter2 <T> List ) {
如果有任何值得强调的其他细节,或者也许一些引用资料来了解更多信息,也将受到欢迎。
最佳答案
For example , to see that question did the C # runtime stores the information of the generic types instead of discarding it , unlike Java, is that correct?
是的,这是正确的。 C# 就不会受到这种类型删除的限制。
Also, I have never seen an example in C # that utilizes " jokers " ( wildcards ) and type < ? extends TipoGenerico > . This is possible ( or even necessary ) in that language ?
是的,这是通过 generic constraints 完成的,例如:
public class SomeType<T> where T : IEquatable<T>
{
这将创建一个类,其中类型 T
必须实现IEquatable<T>
.
Finally , C # supports individual methods ( not just classes ) generics ? If yes , what is the equivalent syntax of this construction in Java :
public void Method<T>(T parameter1, List<T> parameter2)
{
关于c# - C# 中的泛型类型与 Java 中的一样吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22567917/