In C#, you can store different types of generic delegates in a List using the Action
and Func
classes.
The Action
class represents a delegate that does not return a value and can take up to 16 input parameters. The Func
class, on the other hand, represents a delegate that returns a value and can take up to 16 input parameters.
Here's an example of how to store different types of generic delegates in a List using Action
and Func
:
main.cs665 chars27 lines
In this example, we create three different generic delegates: del1
of type Action<string>
, del2
of type Func<int, int, int>
, and del3
of type Action<int, string>
. We then add these delegates to a List called delegates
.
To invoke the delegates in the List, we loop through each delegate and use the is
operator to determine its type. We then cast the delegate to its appropriate type and invoke it accordingly.
Note that this method does not use DynamicInvoke
, which is useful for performance reasons as it avoids the overhead of invoking delegates using a reflection-based mechanism.
gistlibby LogSnag