Deep C# - Delegates |
Written by Mike James | ||||||||
Monday, 02 June 2025 | ||||||||
Page 4 of 4
Generic DelegatesTo confuse matters even more, or should that be to confer further elegance and power, you can also create generic delegate types. Put simply, you can use a generic type anywhere within a delegate definition. For example:
creates a delegate type with a generic parameter. To use the delegate you have to provide the type information. For example:
where
Before you start to invent clever ways of creating generic delegates for every purpose, I should warn you that, in the main, generic delegates should only be used within generic classes. In this context they provide a way to create delegate instances that “fit in” with the functioning of the entire delegate class. For example, you might create a delegate as a notification or event method which can be customized to work with the same data types as the rest of the generic class. PostludeDelegates are a central part of the .NET framework, but since .NET 2.0 it has been possible to save some typing and avoid unnecessary names by using anonymous, as opposed to named, methods. However as of .NET 3.5 anonymous methods have been overshadowed by lambda expressions. In both cases you can simply treat the new facilities as easier ways of defining a delegate. Anonymous methods are covered in depth in Chapter 14 together with lambda expressions. Also of interest is the way anonymous methods and lambda expressions enforce the adoption of closure, which is also discussed in that chapter. Deep C#Buy Now From Amazon Chapter List
Extra Material
To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info |
||||||||
Last Updated ( Monday, 02 June 2025 ) |