Tuesday, June 14, 2011
Evolution Of Delegates from .NET 1.x to 3.5 including Code samples.
Delegates were introduced with C# 1.0 (with .NET 1.0) as a way to represent function pointers. Delegates like most other aspects of advanced C#/.NET programming were often misunderstood and a lot of times programmers didn't even know they were using them inside their code! One source of this problem was a rather cluggy syntax provided with the original C# 1.0 syntax. Luckily as .NET evolved, so did the syntax for delegates by becoming more concise and clear. This is exactly what this article focuses on; this is NOT an intro into delegates, how they relate/work with events or a "best practice" guide.
The best way to show delegate syntax in action is to try to solve a simple coding problem with them. Suppose I get asked this on an interview: I have a form with 2 text boxes and I can input 2 numbers. What I would like to do is when I click the "=" button (equals) is have the numbers be added via a delegate binary operation, by adding the two numbers.