Chris Donnan : Programming - Brooklyn Style
software, trading, family, fun
Posted c#, programming on Monday, February 19th, 2007.
Puzzle:
What will it print?

Well - it calls the object overload all 3 times…
Lets see the IL

So - you see that it does the cast to !T (generic parameter T) but it STILL calls the ‘object’ override in L_0008. I have played and played with this. There is another IL syntax that shows !1, etc for the VALUE OF generic parameter 1 I believe, but the IL does not get generated to call the object override.
This is a somewhat contrived example, but I have on a few times now (this is why I am complaining at this point) tried to use a genric as a parameterized type for a block of code that needed simply to have that - a parameterized type. That said - a generic value WILL NEVER call the polymorphic overrides - it will always use object. It seems that there is IL that COULD be generated (the !1 - etc. syntax), but it is not.
So - there is my rant. Generics are a great tool, but sometimes they can cause a headache
-Chris
One Response to “Generics are odd at times in .net”
Comment on this post below
You must be logged in to post a comment.
You can leave a response, or trackback from your own site.
















I think this is because of the same reason why overloaded operators (+/-, etc) don’t work with generic types. While polymorphinism and generics are evaluated at runtime, method overload is evaluated at compile time.