The code first defines the four functions F1, F2, F3, and F4. The four CheckBox CheckedChanged event handlers call the DrawGraph method, passing that method the function to graph. The DrawGraph method (which isn't really important to this example, so it's not shown here) takes as a parameter the function to draw and draws it, at one point passing the function to another method.
The fact that their names are F1, F2, F3, and F4 is a hint that the functions themselves aren't really meaningful in themselves. They are basically defined, passed into DrawGraph, and then forgotten.
In that case, you can replace these functions with anonymous methods. An anonymous method is basically a delegate that refers to some code that doesn't have a name. You can store the delegate in a delegate variable or pass it to a method as a parameter.
The syntax for an anonymous method is:
delegate(parameters) { ...code... });
Here parameters defines any parameters the delegate should take. If the code inside the braces returns a value, it should use a return statement just like any other method.
The following code shows how this post's example uses anonymous methods to handle its CheckBoxes' CheckedChanged events.
In this version, the event handlers pass the four functions to the DrawGraph method as anonymous methods so the program doesn't need to define the functions separately. This makes the code a bit more concise and easier to read, although the third anonymous method is a bit long. If that function were much longer, it might be better to put its code in a named method to make the code more readable.
Usually you shouldn't use an anonymous method if the method will be used in multiple places, if it's code is too long, or if it would otherwise be less confusing to use a named method. Anonymous methods don't give you any real performance advantage (unlike the inline macros used by some programming languages) so you should only use them if they make the code easier to understand.
In the next post I'll show how you can use lambda statements to make defining anonymous methods easier.
Comments