13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

110 CHAPTER 3 Parameterized typ<strong>in</strong>g with generics3.6.5 Comparison with Java genericsWhere C++ <strong>in</strong>cludes more of the template <strong>in</strong> the generated code than <strong>C#</strong> does, Java<strong>in</strong>cludes less. In fact, the Java runtime doesn’t know about generics at all. The Javabytecode (roughly equivalent term<strong>in</strong>ology to IL) for a generic type <strong>in</strong>cludes someextra metadata to say that it’s generic, but after compilation the call<strong>in</strong>g code doesn’thave much to <strong>in</strong>dicate that generics were <strong>in</strong>volved at all—and certa<strong>in</strong>ly an <strong>in</strong>stance ofa generic type only knows about the nongeneric side of itself. For example, an<strong>in</strong>stance of HashSet doesn’t know whether it was created as a HashSet ora HashSet. The compiler effectively just adds casts where necessary and performsmore sanity check<strong>in</strong>g. Here’s an example—first the generic Java code:ArrayList str<strong>in</strong>gs = new ArrayList();str<strong>in</strong>gs.add("hello");Str<strong>in</strong>g entry = str<strong>in</strong>gs.get(0);str<strong>in</strong>gs.add(new Object());and now the equivalent nongeneric code:ArrayList str<strong>in</strong>gs = new ArrayList();str<strong>in</strong>gs.add("hello");Str<strong>in</strong>g entry = (Str<strong>in</strong>g) str<strong>in</strong>gs.get(0);str<strong>in</strong>gs.add(new Object());They would generate the same Java bytecode, except for the last l<strong>in</strong>e—which is valid<strong>in</strong> the nongeneric case but caught by the compiler as an error <strong>in</strong> the generic version.You can use a generic type as a “raw” type, which is equivalent to us<strong>in</strong>gjava.lang.Object for each of the type arguments. This rewrit<strong>in</strong>g—and loss of <strong>in</strong>formation—iscalled type erasure. Java doesn’t have user-def<strong>in</strong>ed value types, but you can’teven use the built-<strong>in</strong> ones as type arguments. Instead, you have to use the boxed version—ArrayListfor a list of <strong>in</strong>tegers, for example.You may be forgiven for th<strong>in</strong>k<strong>in</strong>g this is all a bit disappo<strong>in</strong>t<strong>in</strong>g compared withgenerics <strong>in</strong> <strong>C#</strong>, but there are some nice features of Java generics too:■■■■The runtime doesn’t know anyth<strong>in</strong>g about generics, so you can use code compiledus<strong>in</strong>g generics on an older version, as long as you don’t use any classes ormethods that aren’t present on the old version. Version<strong>in</strong>g <strong>in</strong> .NET is muchstricter <strong>in</strong> general—you have to compile us<strong>in</strong>g the oldest environment you wantto run on. That’s safer, but less flexible.You don’t need to learn a new set of classes to use Java generics—where a nongenericdeveloper would use ArrayList, a generic developer just uses Array-List. Exist<strong>in</strong>g classes can reasonably easily be “upgraded” to generic versions.The previous feature has been utilized quite effectively with the reflection system—java.lang.Class(the equivalent of System.Type) is generic, whichallows compile-time type safety to be extended to cover many situations <strong>in</strong>volv<strong>in</strong>greflection. In some other situations it’s a pa<strong>in</strong>, however.Java has support for covariance and contravariance us<strong>in</strong>g wildcards. For<strong>in</strong>stance, ArrayList

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!