Deep C# - Delegates
Written by Mike James   
Monday, 02 June 2025
Article Index
Deep C# - Delegates
The C# Approach
Delegate Patterns
Generic Delegates

Generic Delegates

To 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:

delegate int HelloType<T>(T param);

creates a delegate type with a generic parameter.

To use the delegate you have to provide the type information. For example:

HelloType<int> HelloInst = new HelloType<int>(hello);

where Hello is a method which returns an int and has a single int parameter. You can also use the shorter:

HelloType<int> HelloInst = hello;

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.

Postlude

Delegates 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

DeepCsharp360

 Chapter List

  1. Why C#?
    I Strong Typing & Type Safety
  2. Strong Typing
       Extract 
    Why Strong Typing
  3. Value & Reference
  4.    Extract Value And Reference
  5. Structs & Classes
       Extract
    Structs & Classes 
  6. Inheritance
      
    Extract
    Inheritance
  7. Interfaces & Multiple Inheritance
      
    Extract Interface
  8. Controlling Inheritance
    II Casting & Generics
  9. Casting - The Escape From Strong Typing
      
    Extract Casting I
  10. Generics
  11. Advanced Generics
  12. Anonymous & Dynamic
    Typing
    III Functions
  13. Delegates ***NEW!
  14. Multicast Delegates
  15. Anonymous Methods, Lambdas & Closures
    IV Async
  16. Threading, Tasks & Locking
  17. The Invoke Pattern
  18. Async Await
  19. The Parallel For
    V Data - LINQ, XML & Regular Expressions
  20. The LINQ Principle
  21. XML
  22. LINQ To XML
  23. Regular Expressions
    VI Unsafe & Interop
  24. Interop
  25. COM
  26. Custom Attributes
  27. Bit Manipulation
  28. Advanced Structs
  29. Pointers 

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.

Banner


Konrad Zuse Born On This Day In 1910
22/06/2025

Today marks the 115th anniversary of Konrad Zuse. Although his name may not be familiar in English-speaking circles, in Germany he is known as "der Vater des Computers". He does indeed have a strong c [ ... ]



Google Releases Python Client For Data Commons
01/07/2025

Google has released a new Python client library for Data Commons based on the V2 REST API. They say the new library enhances how data developers can make use of Data Commons.


More News

pico book

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Monday, 02 June 2025 )