Powered by Twitter Tools.

February 2007
M T W T F S S
« Jan   Mar »
 1234
567891011
12131415161718
19202122232425
262728  
Chris Donnan

Create Your Badge

Chris Donnan : Programming – Brooklyn Style

software, trading, family, fun

1 Generics are odd at times in .net

Puzzle:

What will it print?

generics

Well – it calls the object overload all 3 times…

Lets see the IL

genericsil.jpg

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

Share and Enjoy:
  • del.icio.us
  • digg
  • blinkbits
  • BlinkList
  • blogmarks
  • YahooMyWeb
  • connotea
  • De.lirio.us
  • Fark
  • Furl
  • Reddit
  • scuttle
  • Shadows
  • Smarking
  • Spurl
  • TailRank
  • Wists

One Response to “Generics are odd at times in .net”

  1. mchen, on March 2nd, 2007 at 5:49 pm, said:

    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.

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.