The Artima Developer Community
Interviews | Discuss | Print | Email | First Page | Previous | Next
Sponsored Link

Generics in C#, Java, and C++
A Conversation with Anders Hejlsberg, Part VII
by Bill Venners with Bruce Eckel
January 26, 2004

Page 1 of 3  >>


Anders Hejlsberg, the lead C# architect, talks with Bruce Eckel and Bill Venners about C# and Java generics, C++ templates, constraints, and the weak-strong typing dial.

Anders Hejlsberg, a distinguished engineer at Microsoft, led the team that designed the C# (pronounced C Sharp) programming language. Hejlsberg first vaulted onto the software world stage in the early eighties by creating a Pascal compiler for MS-DOS and CP/M. A very young company called Borland soon hired Hejlsberg and bought his compiler, which was thereafter marketed as Turbo Pascal. At Borland, Hejlsberg continued to develop Turbo Pascal and eventually led the team that designed Turbo Pascal's replacement: Delphi. In 1996, after 13 years with Borland, Hejlsberg joined Microsoft, where he initially worked as an architect of Visual J++ and the Windows Foundation Classes (WFC). Subsequently, Hejlsberg was chief designer of C# and a key participant in the creation of the .NET framework. Currently, Anders Hejlsberg leads the continued development of the C# programming language.

On July 30, 2003, Bruce Eckel, author of Thinking in C++ and Thinking in Java, and Bill Venners, editor-in-chief of, met with Anders Hejlsberg in his office at Microsoft in Redmond, Washington. In this interview, which is being published in multiple installments on and on an audio CD-ROM to be released by Bruce Eckel, Anders Hejlsberg discusses many design choices of the C# language and the .NET framework.

Generics in General

Bruce Eckel: Can you give a quick introduction to generics?

Anders Hejlsberg: Generics is essentially the ability to have type parameters on your type. They are also called parameterized types or parametric polymorphism. The classic example is a List collection class. A List is a convenient growable array. It has a sort method, you can index into it, and so on. Today, without parameterized types, there's a tension between either using arrays or Lists for your collections. If you use an array, you get strong typing, because you can declare an array of Customer, but you don't get growability or the convenience methods. If you use a List, you get all the conveniences, but you lose the strong typing. You can't say on a List what it's a List of. It's just a List of Object. And this gives you problems. Types must be checked at runtime, which by inference also means that types aren't checked at compile time. The compiler is perfectly happy to let you stick a Customer in a List and try to take out a String. You don't find out until runtime that it's not going to work. Also, when you put primitive types in a List, you have to box them. Because of all these issues, there's constantly this tension between Lists and arrays. You are always struggling to decide which you should pick.

The great thing about generics is now you can have your cake and eat it to, because you can define a List<T> [pronounced: List of T]. When you use a List, you can actually say what the List is a List of, and have the compiler strongly type check it for you. So that's the immediate benefit. And then there's all sorts of other benefits. Of course, you don't want to do this only with Lists. A Hashtable, or Dictionary—whatever you want to call it—maps keys to values. You may want to map Strings to Customers, or ints to Orders, and you want to be able to say that in a strongly typed fashion.

Page 1 of 3  >>

Interviews | Discuss | Print | Email | First Page | Previous | Next

Sponsored Links

Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use