2

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#.

mckamey
  • 17,359
  • 16
  • 83
  • 116

3 Answers3

2

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 C# vs Java generics

Community
  • 1
  • 1
Kelly Robins
  • 7,168
  • 6
  • 43
  • 66
1

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
  • 14,496
  • 5
  • 31
  • 37
  • 2
    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 Aug 08 '09 at 02:40
1

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

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186