Andy's observations as he continues to attempt to know all that is .NET...

Monday, July 03, 2006

Delegate binding in .NET 2

In .NET 1.1 a delegate defined like

delegate void Proc(string foo);

Could only be instantiated with a method which had that exact signature. In .NET 2.0 they relaxed it so that a method defined as

void MyProc( object bar );

Could be wrapped by the above delegate, since MyProc parameter is more restrictive, which makes sense.However what you can't do is

delegate void Proc(int foo);

And make it wrap up

void MyProc(object bar );

The compiler will not let you...Hold on, int ultimately derives from System.Object so why not. My guess is that it comes down to boxing, since in order to make code work the method MyProc must see a reference type. The code invoking the delegate, or the CLR generated code would have to be smart enough to see that the actual implementation was expecting a reference type and perform a boxing operation on the parameters being passed. So perhaps the seamless type system is starting to show a few cracks....

1 comment:

Anonymous said...

Hey just stopping by to say I like the blog.

I don't know if your also into Jujitsu but I got sites and Squidoo lenses on Jujitsu if your intested:

Jujitsu
Jujitsu For Women
Japanese Jujitsu
Jujitsu Philosophy

Hope you like them and I will come by again!

About Me

My photo
Im a freelance consultant for .NET based technology. My last real job, was at Cisco System were I was a lead architect for Cisco's identity solutions. I arrived at Cisco via aquisition and prior to that worked in small startups. The startup culture is what appeals to me, and thats why I finally left Cisco after seven years.....I now filll my time through a combination of consultancy and teaching for Developmentor...and working on insane startups that nobody with an ounce of sense would look twice at...