C# Generics

by William Grosso

Related link: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/ht…



I went to SDForum's Windows SIG last night, to learn about Generics in C#. It was interesting and I recommend that anyone interested in generics download the whitepaper (in particular, as a Java guy, I was fascinated by how different the design decisions were).

4 Comments

jimothy
2004-02-05 11:57:17
Interesting
I've yet to program in C#, but the more I read about it, the more I like it (except for the initial capitals in Method() names).


In particular, I find it interest that generics in C# are designed to improve performance as well as type safety:



However, there are two problems with Object-based solutions. The first issue is performance. When using value types, you have to box them in order to push and store them, and unbox the value types when popping them off the stack. Boxing and unboxing incurs a significant performance penalty in their own right, but it also increases the pressure on the managed heap, resulting in more garbage collections, which is not great for performance either. Even when using reference types instead of value types, there is still a performance penalty because you have to cast from an Object to the actual type you interact with and incur the casting cost:


As far as I'm aware, generics in Java are strictly to improve type safety and coding convenience.


P.S. What's the deal with their linked list example? Perhaps I'm naive, but I've never seen a key-value pair linked list before.

wegrosso
2004-02-05 15:41:52
Interesting
Generics in Java aren't about performance; that's true.


And the talk I went to last night demonstrated that, in a tight loop that does nothing but pull things in and out of a IList, you can make the code run twice as fast by using generics and eliminating the casts in C#. I don't know how impressive that is; I suspect that the total cost of casting (and even of autoboxing) is noise on the performance of an application. But, YMMV.


I don't know enough to talk about whether C# is a nice language but my impression last night was one of inordinate clutter and a lot of ugly code. That could just be me though (I've been a java guy for 7 years now and my taste buds are fine-tuned).


The main thing that bothered me was the lack of backwards compatibility (which is one of the big design points of the Java Generics). If you compile code using generics in Java, it will run on older JVMs. This is not the case in C#-- if you compile C# generics code, it will require .NET 2.0 to run.


Moreover, from talking to developers afterwards, I got the impression that people are frequently forced to upgrade the CLR and that there are a lot of headaches involved. All of which made Joel's whining more plausible to me. The first time I read this article (pre-SIG meeting), I thought it was probably a pointless whine about a minor issue. Post-SIG, I'm thinking he's articulating a very real pain point that's going to significantly hinder .NET deployments.



jimothy
2004-02-06 06:56:06
Interesting
I suppose I also need to admit that I'm not qualified to say whether C# is a nice language or not. But I've read somethings about it that do seem nice; I'm sure it has its warts as well. But I'm like you, a Java programmer of seven years, so rest assured I'm no Microsoft cheerleader.


Joel's article on the linker is also an interesting read, particularly because so much of it is applicable to Java. Java also has no linker. This is by design, of course, and enables a lot of powerful features (certain features of reflection, for instance, would be impossible with a linker), but it also leads to a lot of bloat. Written a project that uses any of the Jakarta Commons libraries, for instance? Your application swells because even if you use only a few classes and methods, you're required to include the entire library, and all the libraries that that library depends upon, and all the libraries THEY depend on...


One cause (far from the sole cause) for Java's slow adoption on the desktop, I believe, is that a modern version the JRE isn't installed by default on the dominant operating system, and that users must download a HUGE installer. This is the same concern Joel has for .NET.


Like Joel, I've reasoned that as time goes on, more and more users will have Java installed. But as he points out for .NET, Sun comes out with periodic new releases of the JRE, requiring a new download. All with the same potential for version conflicts as Joel raises for .NET.


I'm doing one of those things I hate to do; complaining about things but not really offering a solution. Perhaps I don't even intend to complain, just raise a few points. Or perhaps I'm just ranting. Whatever the case, I'm still a Java fan.

wegrosso
2004-02-06 08:31:53
It's not as bad with Java
Well, there are two mitigating factors for the linker in Java. One is that the JREs are much more backwards compatible-- it means you don't need to install or update as often.


The second is a side effect of the backwards compatibility-- it's possible to do a very good job at native compilation. If you go to my website> and look at the presentations page, you'll see a presentation on native compailation that goes into "solving the linker and installation problem" in depth (and talks about why Seruku used native compilation).