0

Is it possible to do something like this?

System.Collections.Generic.Queue<delegate> methods;

Currently I'm doing this...

public delegate void Method();

System.Collections.Generic.Queue<Method> methods;

However, this only allows me to enqueue methods that match the delegate signature. I'd like to be able to put methods with any signature in the queue, or at least methods with different parameters. For example, I'd like to be able to do something like this...

methods.Enqueue(MethodOne);
methods.Enqueue(MethodTwo(int parameter)); //This won't work like this, since 'void' will be enqueued instead of the method.

Is something like this possible?

Tester101
  • 8,042
  • 13
  • 55
  • 78

1 Answers1

3

This is possible:

    List<Delegate> list = new List<Delegate>();
    list.Add(new Action(() => Console.WriteLine("Hello world")));

but adding a lambda expression directly doesn't seem to work.

Dennis_E
  • 8,751
  • 23
  • 29
  • I think adding the lambda will work if you cast it. See, for example, http://stackoverflow.com/a/411597/56778 – Jim Mischel Jun 05 '14 at 13:44
  • Indeed, a lambda expression directly isn't a delegate. In the example you provide it's possible to add a Func or an Action – Complexity Jun 05 '14 at 13:44