Anonymous Methods

An anonymous method is a nameless method. Sometimes a function is used exclusively with a delegate. It is never called directly as a stand-alone method. Without an anonymous method, you must define a separate function as a member of a class. The function then is assigned to the delegate. Using an anonymous method prevents creating a separate method unnecessarily, which is cleaner and more convenient than using a named method. Anonymous methods can be substituted for delegates in most circumstances. Lambda expressions might make anonymous methods irrelevant. However, anonymous methods remain an artifact of the C# language. Lambda expressions were introduced in Chapter 1.

Define an anonymous method with the delegate keyword and ...

Get Programming Microsoft® Visual C#® 2008: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.