Prev | Current Page 224 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"

aspx.
The other obvious language to compare with C# in terms of generics is Java, which
introduced the feature into the mainstream language for the 1.5 release,17 several
years after other projects had compilers for their Java-like languages.
15 http://en.wikipedia.org/wiki/Template_metaprogramming
16 The inventor of C++.
17 Or 5.0, depending on which numbering system you use. Don??™t get me started.
110 CHAPTER 3 Parameterized typing with generics
3.6.5 Comparison with Java generics
Where C++ includes more of the template in the generated code than C# does, Java
includes less. In fact, the Java runtime doesn??™t know about generics at all. The Java
bytecode (roughly equivalent terminology to IL) for a generic type includes some
extra metadata to say that it??™s generic, but after compilation the calling code doesn??™t
have much to indicate that generics were involved at all??”and certainly an instance of
a generic type only knows about the nongeneric side of itself. For example, an
instance of HashSet doesn??™t know whether it was created as a HashSet or
a HashSet. The compiler effectively just adds casts where necessary and performs
more sanity checking. Here??™s an example??”first the generic Java code:
ArrayList strings = new ArrayList();
strings.add("hello");
String entry = strings.get(0);
strings.


Pages:
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236