Generics Experience

7/6/2004 7:02:08 AM

Generics Experience

I made the jump to the Whidbey and am not looking back. The IDE, the compiler and the framework are all solid. I have decided to use the advanced C# features and any new functionality in .NET framework. What this means is that my product release date is now tied to the release date of Whidbey, which could as be as long as one year away. My main reason for abandoning the possibility of returning to VS 2003 is generics. Generics makes it very difficult to go back to an earlier framework.

I have started moving from my homegrown template solution to generics. If you use generics, especially with collections, you'll notice that, unlike the other new features in C#, generics is viral. It will permeate itself throughout the whole of your project. The other features have local impact in the class in which it is defined, but generics, which is really a runtime feature and not a compiler feature, has widespread impact.

Any reference to a generic type or method must used the new syntax. If a class that uses another generic type exposes the generic results in its interface, other classes that refer to that class will require the new syntax and similarly be affected. Add generics to one file and the compiler complains, forcing changes to referring files. The polluting nature of generics reminds me of the C++ "const" keyword.

I recommend just using the generic version of the Collections namespace rather than supporting both generic and non-generic interfaces. The System.Collection class is essentially obsolete. If you need to expose a non-generic collection interface such as IList, ICollection, IEnumerable, then create a wrapper class that wraps an IList<T> (and so on) and exposes the IList interface (and possible an IList<object>, since generics don't support covariance).

My main gripes with generics is that

  • operators, especially equality, are not supported...
  • equality requires using the polymorphic Object.Equals(method), which probably has a boxing hit for value types (though it's technically possibly that this has been optimized) or an IComparable constraint be available
  • lack of specialization

 One way of alleviating the proliferation of generic syntax throughout the code is to derived a specific instance of a generic class and rename it to remove the genericity.






Net Undocumented is a blog about the internals of .NET including Xamarin implementations. Other topics include managed and web languages (C#, C++, Javascript), computer science theory, software engineering and software entrepreneurship.

Social Media