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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access