0

Generic classes can declared with generic delegates as shown below but this is unfortunatelly metadata. I can not find full example:

public abstract class Expression
{
public static Expression<TDelegate> Lambda<TDelegate>(Expression body, bool tailCall, IEnumerable<ParameterExpression> parameters);
}

in here TDelegate is a Func<>. Can you write a generic class example which uses generic delegate? I could not find an example like that.

1 Answers1

0

Generic parameters wouldn't help much when constraining delegate types. There's no practical use case for this unless you want to pass a delegate type using a generic argument instead of using regular typeof keyword or Object.GetType method:

public void DoStuff<TDelegate>()
// vs.
public void DoStuff(Type delegateType);

Delegates aren't classes and they don't support inheritance and other features available to classes and interfaces. What you can do is constraining delegate's generic parameters.

For example, you want a Func<T> to constrain that T is IWhatever:

public void DoStuff<T>(Func<T> func) where T : IWhatever 
{
}
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206