Anonymous Methods in C#
The introduction of anonymous methods in C# 2.0 gives developers the ability to efficiently design code that makes use of delegates in callback situations. Prior to this language enhancement, a developer would have to define a separate method when needing to execute a callback using a delegate. Indeed, while not having to define a separate method to execute callbacks is certainly a nicety, that reason alone does not fully qualify the value of a new language feature such as anonymous methods. As we will see in the Web Resource Information Reader sample, the real benefit of anonymous methods can be seen in the flexibility it offers developers by allowing them to execute callbacks, synchronously or asynchronously, within its containing method code. When using anonymous methods, a developer can still reference local variables scoped for the containing method from within the anonymous method definition. Let's get started by showing how anonymous method in C# 2.0 has changed the way code can be written for callback situations.