C# for Java Programmers

Interaction between the user and the computer is a fundamental part of computing. In the old days we used a procedural programming model, like a command line program that asks for user input one line (or character) at a time. Nowadays we are treated to more sophisticated programs with rich graphical user interfaces (GUI) and event-driven programming. To facilitate writing event-driven programs, a language must provide support for event handling. Java provides this by using inner class adapters in its Abstract Windowing Toolkit (AWT) event-handling model. C#, on the other hand, implements event handling through delegates.
A delegate is essentially a reference to some method. When it is declared, it is assigned a method signature. An application can then assign any method that matches this signature to a delegate variable. When this delegate variable is invoked, the associated method is called. If you have some C/C++ background, think of delegates as being very similar to function pointers. However, unlike function pointers, delegates are object-oriented, type-safe, and secure.
In the first part of the chapter we will look at delegates and how they can be used as callback methods. Next we ll talk about how delegates are used for events and event handling. Finally, we ll go over some more advanced delegate concepts.
As previously mentioned, a delegate is a reference to some method. It is much like any other variable, except that it represents a method. For instance, an integer type can hold any value that fits the description...