free geoip First thoughts on C# Generics... - Jayson's Blog - jaysonKnight.com
jaysonKnight.com
A conduit to the voices inside my head.
First thoughts on C# Generics...
as i'm sure everyone has seen, the next version of C# will include compile time support for super late binding...also known in the OO world as Generics. since even the early VB days, in order to achieve this type of functionality (albeit at run-time), we would use variant types, or in the .NET world, object types...then just supply several overloaded methods based on the actual type of object passed in. not only does this lead to severely repetitious coding, there is a severe performance penalty involved w/ casting, and in a lot of cases, boxing/unboxing. there is a really cool tool dev'd by MS at this link: this will show you just where the perf penalties are. in most cases, next to casting from object types, string concat's are 2nd in the performance hit category (see my earlier post on the StringBuilder object). this tool is basically a query analyzer type tool, but for managed code.

anyway, on to generics. it's quite the buzz in the managed world of .NET right now, and for good reason. instead of expensive runtime checks, and the associated non-type safety associated w/ those checks, we can now define methods that in effect supply the same runtime functionality of supplying object types and casting to the correct type...AT COMPILE TIME! this is a godsend to component developers, and the amount of redundant code this will save is pretty incredible, this will have a HUGE impact on polymorphism. i can think of literally hundreds of classes i've written that could benefit from generics, and code bloat would shrink dramatically.

take for example the framework i wrote for tricking class libraries into thinking that they have a .config file. there are several way to let this class lib know where it lives so that it knows where to search for a .config file (path reference, assembly reference, etc), however each one of these requires a seperate cast from a type of object to the desired type. you can write overloaded methods for each type, or write a generic method accepting an object ref, then testing whatever is passed in to see exactly what the heck it is (and truth be told, there is probably some way that someone would want to pass a reference in that i haven't thought of), then perform the appropriate cast to the type you need. that is very tedious work, trying to predict what other coders are going to do...so why even attempt it? generics to the rescue! :-) in a future post i will re-write my .config class library to take full advantage of generics...it will have to wait for my copy of whidbey to get here though. i will also be posting soon on the other additions to the C# language...

more to come...
j

Posted Tue, Nov 18 2003 12:07 AM by Jayson Knight
comments powered by Disqus

Comments

Juan Wajnerman wrote re: First thoughts on C# Generics...
on Sat, Mar 20 2004 12:30 AM
Generics are a cool feature, but I was expecting more from them. Maybe you want to read my post:
http://weblogs.manas.com.ar/waj/archive/2004/03/16/146.aspx

Copyright © :: JaysonKnight.com
External Content © :: Respective Authors

Terms of Service/Privacy Policy