Recent Posts
Calendar
December 2008
S M T W T F S
« Nov   Jan »
 123456
78910111213
14151617181920
21222324252627
28293031  

Archive for December 4th, 2008

Delegates in .NET

hand-010 I received the following question:

What is a delegate?  What problem does it solve?  and When might I use a delegate?

A delegate is essentially a function pointer.  We have used function pointers in various scenarios in the past to solve the problem of needing to execute user-defined code inside of another function or to fire events using the observed/observable programming pattern.

The problem we’ve always had with function pointers in the past is that there has never been any type safety.  If I had a variable that was supposed to point to a function, I could assign it any function.  So if the function the code is expecting returns a string and takes an integer and a string as parameters, I could assign the variable a function that returned nothing (void) and passed nothing.  My code would compile, but when I got to that function, the chances are my code would blow up on me.

Read the rest of this entry »