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

Extra Material
Passing Parameters ***NEW!

 <ASIN:B09FTLPTP9>

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


Meet Reo.Dev, the Developer Intent Platform
08/10/2025

Selling software to engineering teams has always been a challenge for developer tools companies. Formal sales cycles often lag months behind a developer’s initial, silent adoption of a tool. To addr [ ... ]



js1024 Revisited in 2025
12/09/2025

After a few years we revisit the fun annual JavaScript Golfing competition. So what's new?


More News

pico book

 

Comments




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



Last Updated ( Monday, 02 June 2025 )