Delegate Variety in C#

added by bartm
3/22/2012 8:43:57 AM


One of the constructs many junior programmers struggle with is the delegate. Coming from a world of Visual Basic or Java, many have never seen a function pointer (as a C programmer would every day), so the concept of referencing a function or method in a variable is completely foreign to them. In the early days of C#, delegates had to be instantiated with named methods and were somewhat cumbersome. Today, anonymous delegates and lambda expressions are profuse in many popular frameworks and APIs, so it's important to take a bit of time to cement these concepts into our brains. To help some VB6 programmers adapt to C# and the many equivalent flavors of delegates, I walked through some simple samples to show them the different flavors of delegates. Below is a complete sample showing a few different ways to declare delegates in C#.