Superb article on Generics in C# & Java

Bill Venners posted another great article on artima.com, this one the 7th in a series of conversations with Anders Hejlsberg. If you’re not familiar with generics, Anders provides a quick introduction and then dives into explaining how C# generics were implemented and compares that implementation to Java’s implementation in Tiger.

Specifically, Java’s implementation doesn’t “… get any of the execution efficiency that I talked about, because when you compile a generic class in Java, the compiler takes away the type parameter and substitutes Object everywhere.” Additionally, the Java implementation loses the generic type information at runtime, which means that performing reflection on a generic leaves you with an array or a List of type ‘Object’, rather than the type specificed at compile time.

One of the takeaways appears to be that the Java team made it a design goal to enable generics to run on an unmodified VM (ie: they cared about backward compatability) while the Microsoft team made their changes with knowing that they could simply release a new version of the .NET runtime, with no regard to making it backward compatible. (am I right?) So in this case it seems that having a smaller installed base enables the .NET team to make more radical changes to their product, effectively alienating a smaller group of people than the Java team, who must consider a large install base and a large group of alienated users.

3 thoughts on “Superb article on Generics in C# & Java”

  1. What I don’t understand about his interview is that he said you don’t get any efficiency out of Java’s, because it just casts. However, he went on to say that C#’s implementation only creates a single class in memory, for Object, and then generic instances of the class cast from Object.

    Also, I read that a lot of work has gone into the JVM to be able to delete casts at runtime for uses of generic classes whose generic types can be guaranteed (which I think is most uses, but excludes things like using a generic list that you didn’t create, or passing a list to an external method that casts it down to the non-generic type).

    Also, Anders is a very good Microsoft representative – when asked if C#’s generics allowed generic methods, which it doesn’t, he spent a few paragraphs changing the subject, and never quite clearly answered the question.

  2. Keith,

    I’m by no means an expert, but I believe your second paragraph answered the question in your first. In the Whidbey CLR, the generic types can *always* be guaranteed, so the casts can always be deleted.

    In any case, I don’t think Hejlsberg was referring to downcasts when he spoke of Java generics lacking execution efficiency. I think he is talking about the boxing of primitives–which Java generics need to do, and C# generics don’t. That costs both time and space.

    -jmc

  3. Keith,

    Are you sure C# does not have generic methods? I just fired up Visual Studio 2005 and wrote this:

    class Program
    {
    static void Main(string[] args)
    {

    }

    public static void Print(T obj)
    {
    System.Console.WriteLine(obj.ToString());
    }
    }

    Guess what… It works like a charm. Is this sort of situation what you’re reffering to?

Leave a Reply to Ovidiu Cancel reply

Your email address will not be published. Required fields are marked *