views:

124

answers:

3

Are there any good quick reference guides out there (preferably online) for Java Generics from the perspective of someone with complete understanding of C# Generics? I find that the syntax is so similar that I expect it to just work and then I run into unfamiliar Java syntax like this:

Class<?> foo;

which I thought was similar to the C#:

Type<T> foo;

but that doesn't really seem to make sense in the context that I'm seeing it. Especially since there is a lack of context for T like there would be in C#.

+1  A: 

http://www.jprl.com/Blog/archive/development/2007/Aug-31.html has a fairly good post on the topic.

There is a post here as well with some further discussion that may be helpful http://stackoverflow.com/questions/355060/c-vs-java-generics

apocalypse9
That was a helpful link. I think this answer satisfies the overall question most closely.
McKAMEY
+1  A: 

What you ask is called unbound wildcards. Unfortunately C# doesn't support them. Also C# 2.0-3.5 has no implementation of covariant and contravariant generics. Latter are introduced C# 4.0. Instead of Class<?> foo; you can write Class<object> foo; in C#. Basically ? was introduced in Java generics for backward compatibility with older versions.

Short answer is no, you don't have direct representation of ? in C#, I'd suggest you to replace it with object type, or make it template converting the whole class to template class.

Sorantis
I appreciate the detail about the specific example. In C# though, `Class` is called `Type` and it isn't generic so you can't add a generic parameter to it. So if I'm understanding correctly, the closest C# match for `Class<?>` would be `Type`.
McKAMEY
+1  A: 

Tutorial from sun http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

adatapost
Good link to have as definitive reference. Thanks.
McKAMEY